#P10178. 陌路寻诗礼

    ID: 11510 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>洛谷原创Special JudgeO2优化广度优先搜索 BFS最短路洛谷月赛

陌路寻诗礼

Background

As a Luogu internet celebrity, Fan Ju has many passionate fans, and he also really enjoys meeting up in person, searching for his fans all over the country.

Problem Description

The map of Fan Ju's hometown is a directed graph with nn nodes and mm directed roads. Each node is a city. Fan Ju is in city 11, and city 11 can always reach any other city through some roads.

The ii-th road goes from city xix_i to city yiy_i with length ziz_i.

Now Fan Fan wants to travel from his city 11 to every city to meet fans in person.

Since Fan Fan is skilled at the SPFA algorithm, during these meetups he will naturally go to other cities along the shortest paths in terms of total length.

However, Fan Fan has trouble making choices. He hopes that the shortest path from city 11 to every city is unique, so he decides to cast magic to change the lengths of all roads. Specifically:

After casting magic, the length of each road can be changed independently into an integer in the range [1,k][1,k], where kk is Fan Ju's magic level.

But the map of his world and his magic level keep changing. In total there will be TT changes, so he wants you to give a construction method for each of the TT queries so that Fan Ju will not be troubled by choices, or report that there is no solution.

Input Format

The first line contains an integer TT, the number of test cases.

Then for each of the TT test cases, the first line contains three integers n,m,kn,m,k, followed by mm lines describing directed roads (xi,yi)(x_i,y_i).

Self-loops and multiple edges are not guaranteed to be absent.

Output Format

For each test case, if there is a solution, output Yes on the first line, and output mm numbers on the second line as the weights of the edges in order. If there is no solution, output No on a single line.

This problem uses special judge, meaning if there are multiple valid answers, you may output any one of them.

2
3 2 3
1 2
2 3
2 2 1
1 2
1 2
Yes
1 2
No

Hint

Sample Explanation

For the first test case, the path from node 11 to every node is unique, so no matter how the edge weights are set, the shortest paths are unique.

For the second test case, since k=1k=1, both edges can only be set to weight 11. The shortest path length from node 11 to node 22 is 11, and you can take either of the two edges, so it is not unique.

Constraints

This problem uses bundled testdata.

For 20%20\% of the testdata, n,m5n,m\leq 5.

For another 20%20\% of the testdata, k=1k=1.

For another 20%20\% of the testdata, m=n1m=n-1.

For another 20%20\% of the testdata, k=109k=10^9.

For 100%100\% of the testdata, n1n\ge 1, m0m\ge 0, 1n,m3×1051\le \sum n,\sum m\leq 3\times 10^5, 1k1091\leq k \leq 10^9, and 1xi,yin1\le x_i,y_i\le n.

Translated by ChatGPT 5