#P12042. Graph Path Mex Assignment
Graph Path Mex Assignment
Graph Path Mex Assignment
You are given an undirected graph with n vertices and m edges. The i-th edge connects vertices ui and vi and has an unknown weight ai (a non‐negative integer).
For any path p = (p0, p1, ..., pk) (where each adjacent pair (pi-1, pi) is an edge in the graph — vertices and edges may be repeated), define its cost by first collecting the multiset of weights of the edges in the path. Let the cost be the mex of this multiset, i.e. the smallest non-negative integer that does not appear among them. In mathematical notation, if the edges used (by their indices) are e1, e2, ..., ek, then the cost is
[ \mathrm{mex}{a_{e_1},a_{e_2},\dots,a_{e_k}}. ]
For any two vertices x and y, define f(x, y) as the maximum cost among all paths connecting x and y.
You are given the numbers n and m, followed by m lines. For the i-th edge (ui, vi) the value f(ui, vi) is provided. Your task is to determine whether there exists an assignment of weights ai to the edges such that for every edge (ui, vi) the maximum cost achievable over all paths from ui to vi equals the given value. If a solution exists, you must output one valid assignment.
Note: Since any path may use cycles and repeated edges, if the edges of a connected component receive weights whose union is exactly \(\{0,1,\dots, K-1\}\) (when \(K=f(x,y)>0\)) or do not include \(0\) (when \(K=0\)), then for any two vertices in that component f(x,y) equals \(K\). In a tree (a connected component with only one edge per connection), the only possible values are 0 or 1.
inputFormat
The first line contains two integers n and m (1 ≤ n, m ≤ 105), representing the number of vertices and edges respectively. Each of the following m lines contains three integers u, v, and f, where u and v (1 ≤ u, v ≤ n) are the endpoints of an edge and f (0 ≤ f ≤ 109) is the given value of f(u,v) for that edge.
outputFormat
If a valid assignment exists, output “YES” (without quotes) on the first line. On the second line, output m non-negative integers where the i-th integer represents the assigned weight ai for the i-th edge (in the same order as the input). If there is no valid assignment, output “NO” (without quotes).
sample
3 2
1 2 1
2 3 1
YES
0 0