/
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
頂点に 1,2,\dots,N の番号がついた N 頂点の木があります。ここで、 N は奇数です。
辺にも 1,2,\dots,N-1 の番号がついており、辺 i は頂点 A_i,B_i を結んでいます。
各頂点 i には変数 x_i が設定されており、はじめは全ての頂点 i について x_i=0 です。
これから、 i=1,2,\dots,N-1 について順に以下の操作を行います。
- まず、辺 i を取り除く。その結果、木は 2 つの連結成分に分かれる。
- 含まれる頂点の数がより多い連結成分に含まれる全ての頂点 v について、 x_v に i を加算する。
- なお、 N は奇数なので、含まれる頂点の数がより多い連結成分は一意に定まる。
- 最後に、辺 i を復元する。
全ての操作を行った後の、各頂点に設定された変数の値 x_1,x_2,\dots,x_N を求めてください。
制約
- 入力は全て整数
- N は 3 \le N \le 2 \times 10^5 - 1 を満たす奇数
- 1 \le A_i < B_i \le N
- 与えられるグラフは木である
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
出力
最終的な各変数の値を、以下の形式で出力せよ。
x_1 x_2 \dots x_N
入力例 1
5 2 4 1 5 1 4 1 3
出力例 1
10 6 6 7 8
- i=1 について、以下の操作が行われます。
- 辺 1 を取り除くと、木は頂点 2 からなる連結成分と頂点 1,3,4,5 からなる連結成分とに分かれます。
- より大きいのは頂点 1,3,4,5 からなる連結成分なので、これらに設定された変数すべてに対して 1 を加算します。
- その結果、各変数の値は (x_1,x_2,x_3,x_4,x_5)=(1,0,1,1,1) となります。
- 最後に、辺 1 を復元します。
- i=2 について、以下の操作が行われます。
- 辺 2 を取り除くと、木は頂点 5 からなる連結成分と頂点 1,2,3,4 からなる連結成分とに分かれます。
- より大きいのは頂点 1,2,3,4 からなる連結成分なので、これらに設定された変数すべてに対して 2 を加算します。
- その結果、各変数の値は (x_1,x_2,x_3,x_4,x_5)=(3,2,3,3,1) となります。
- 最後に、辺 2 を復元します。
- i=3 について、以下の操作が行われます。
- 辺 3 を取り除くと、木は頂点 2,4 からなる連結成分と頂点 1,3,5 からなる連結成分とに分かれます。
- より大きいのは頂点 1,3,5 からなる連結成分なので、これらに設定された変数すべてに対して 3 を加算します。
- その結果、各変数の値は (x_1,x_2,x_3,x_4,x_5)=(6,2,6,3,4) となります。
- 最後に、辺 3 を復元します。
- i=4 について、以下の操作が行われます。
- 辺 4 を取り除くと、木は頂点 3 からなる連結成分と頂点 1,2,4,5 からなる連結成分とに分かれます。
- より大きいのは頂点 1,2,4,5 からなる連結成分なので、これらに設定された変数すべてに対して 4 を加算します。
- その結果、各変数の値は (x_1,x_2,x_3,x_4,x_5)=(10,6,6,7,8) となります。
- 最後に、辺 4 を復元します。
最終的な各変数の値は (x_1,x_2,x_3,x_4,x_5)=(10,6,6,7,8) です。
入力例 2
3 1 2 2 3
出力例 2
2 3 1
入力例 3
9 4 6 2 3 1 5 7 8 2 9 5 6 2 6 7 9
出力例 3
20 36 34 28 23 29 23 19 31
Score : 450 points
Problem Statement
There is a tree with N vertices numbered 1,2,\dots,N. Here, N is odd.
The edges are also numbered 1,2,\dots,N-1, and edge i connects vertices A_i and B_i.
Each vertex i has a variable x_i, and initially x_i=0 for every vertex i.
Now, for i=1,2,\dots,N-1 in this order, perform the following operation.
- First, remove edge i. As a result, the tree is split into two connected components.
- For every vertex v in the connected component containing more vertices, add i to x_v.
- Since N is odd, the connected component containing more vertices is uniquely determined.
- Finally, restore edge i.
Find the values of the variables x_1,x_2,\dots,x_N of the vertices after all operations are performed.
Constraints
- All input values are integers.
- N is an odd number satisfying 3 \le N \le 2 \times 10^5 - 1.
- 1 \le A_i < B_i \le N
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
Output
Output the final values of the variables in the following format:
x_1 x_2 \dots x_N
Sample Input 1
5 2 4 1 5 1 4 1 3
Sample Output 1
10 6 6 7 8
- For i=1, the following operation is performed.
- Removing edge 1 splits the tree into a connected component with vertex 2 and a connected component with vertices 1,3,4,5.
- The larger one is the connected component with vertices 1,3,4,5, so 1 is added to all of their variables.
- As a result, the values of the variables become (x_1,x_2,x_3,x_4,x_5)=(1,0,1,1,1).
- Finally, edge 1 is restored.
- For i=2, the following operation is performed.
- Removing edge 2 splits the tree into a connected component with vertex 5 and a connected component with vertices 1,2,3,4.
- The larger one is the connected component with vertices 1,2,3,4, so 2 is added to all of their variables.
- As a result, the values of the variables become (x_1,x_2,x_3,x_4,x_5)=(3,2,3,3,1).
- Finally, edge 2 is restored.
- For i=3, the following operation is performed.
- Removing edge 3 splits the tree into a connected component with vertices 2,4 and a connected component with vertices 1,3,5.
- The larger one is the connected component with vertices 1,3,5, so 3 is added to all of their variables.
- As a result, the values of the variables become (x_1,x_2,x_3,x_4,x_5)=(6,2,6,3,4).
- Finally, edge 3 is restored.
- For i=4, the following operation is performed.
- Removing edge 4 splits the tree into a connected component with vertex 3 and a connected component with vertices 1,2,4,5.
- The larger one is the connected component with vertices 1,2,4,5, so 4 is added to all of their variables.
- As a result, the values of the variables become (x_1,x_2,x_3,x_4,x_5)=(10,6,6,7,8).
- Finally, edge 4 is restored.
The final values of the variables are (x_1,x_2,x_3,x_4,x_5)=(10,6,6,7,8).
Sample Input 2
3 1 2 2 3
Sample Output 2
2 3 1
Sample Input 3
9 4 6 2 3 1 5 7 8 2 9 5 6 2 6 7 9
Sample Output 3
20 36 34 28 23 29 23 19 31