C - Keep Graph Connected Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1 から N の番号がついた N 個の頂点と 1 から M の番号がついた M 本の辺からなる連結な無向グラフが与えられます。 このグラフに多重辺は存在するかもしれませんが、自己ループはありません。

このグラフのそれぞれの辺には 1 以上 N 以下の整数で表されるラベルがついています。 辺 i はラベル c_i がついており、頂点 u_i,v_i を双方向につなぐ辺です。

すぬけ君はそれぞれの頂点に 1 以上 N 以下の整数を書き込んだのち(頂点に書き込まれた整数に重複があっても構いません)、以下の条件を満たす辺のみを残してそれ以外の辺を取り除くことにしました。

条件:辺の両端の頂点に書き込まれた整数を x,y として、x,y のいずれか一方のみが辺についたラベルと等しい

上記の条件を満たさない辺を取り除いたあとのグラフも連結のままであるような頂点への整数の書き込み方を よい書き込み方 と呼びます。よい書き込み方が存在するかどうかを判定し、存在するならばその一例を示してください。

制約

  • 2 \leq N \leq 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq u_i,v_i,c_i \leq N
  • 与えられるグラフは連結
  • 与えられるグラフに自己ループはない

入力

入力は以下の形式で標準入力から与えられる。

N M
u_1 v_1 c_1
\vdots
u_M v_M c_M

出力

よい書き込み方が存在しないならば No を出力せよ。 存在する場合、N 行出力せよ。i 行目には頂点 i に書き込む整数を出力せよ。 この書き込み方がよい書き込み方ならば正解となる。


入力例 1

3 4
1 2 1
2 3 2
3 1 3
1 3 1

出力例 1

1
2
1
  • 頂点 1,2,3 にそれぞれ 1,2,1 を書き込みます。
  • 1 は頂点 1,2 をつないでおり、ラベルが 1 です。
    • 頂点 1 に書き込まれた整数のみが辺についたラベルと等しいため辺 1 は取り除かれません。
  • 2 は頂点 2,3 をつないでおり、ラベルが 2 です。
    • 頂点 2 に書き込まれた整数のみが辺についたラベルと等しいため辺 2 は取り除かれません。
  • 3 は頂点 1,3 をつないでおり、ラベルが 3 です。
    • どちらの頂点に書き込まれた整数も辺についたラベルと異なるため辺 3 は取り除かれます。
  • 4 は頂点 1,3 をつないでおり、ラベルが 1 です。
    • どちらの頂点に書き込まれた整数も辺についたラベルと等しいため辺 4 は取り除かれます。
  • 3,4 が取り除かれたあともグラフは連結なので、この書き込み方はよい書き込み方です。

Score : 500 points

Problem Statement

Given is an undirected connected graph with N vertices numbered 1 to N, and M edges numbered 1 to M. The given graph may contain multi-edges but not self loops.

Each edge has an integer label between 1 and N (inclusive). Edge i has a label c_i, and it connects Vertex u_i and v_i bidirectionally.

Snuke will write an integer between 1 and N (inclusive) on each vertex (multiple vertices may have the same integer written on them) and then keep only the edges satisfying the condition below, removing the other edges.

Condition: Let x and y be the integers written on the vertices that are the endpoints of the edge. Exactly one of x and y equals the label of the edge.

We call a way of writing integers on the vertices good if (and only if) the graph is still connected after removing the edges not satisfying the condition above. Determine whether a good way of writing integers exists, and present one such way if it exists.

Constraints

  • 2 \leq N \leq 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq u_i,v_i,c_i \leq N
  • The given graph is connected and has no self-loops.

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1 c_1
\vdots
u_M v_M c_M

Output

If there is no good way of writing integers, print No. Otherwise, print N lines. The i-th line should contain the integer written on Vertex i. Any good way of writing integers will be accepted.


Sample Input 1

3 4
1 2 1
2 3 2
3 1 3
1 3 1

Sample Output 1

1
2
1
  • We write 1, 2, and 1 on Vertex 1, 2, and 3, respectively.
  • Edge 1 connects Vertex 1 and 2, and its label is 1.
    • Only the integer written on Vertex 1 equals the label, so this edge will not get removed.
  • Edge 2 connects Vertex 2 and 3, and its label is 2.
    • Only the integer written on Vertex 2 equals the label, so this edge will not be removed.
  • Edge 3 connects Vertex 1 and 3, and its label is 3.
    • Both integers written on the vertices differ from the label, so this edge will be removed.
  • Edge 4 connects Vertex 1 and 3, and its label is 1.
    • Both integers written on the vertices equal the label, so this edge will be removed.
  • After Edge 3 and 4 are removed, the graph will still be connected, so this is a good way of writing integers.