F - Distance Sums 2
Editorial
/


Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
頂点の木が与えられます。頂点には の番号がついており、 番目の辺は頂点 を結ぶ無向辺です。
各整数 に対して、 を求めてください。
ただし、 は頂点 から頂点 に到達する際にたどる必要のある最小の辺数です。
制約
- 与えられるグラフは木
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
出力
行出力せよ。
行目には を出力せよ。
入力例 1Copy
Copy
3 1 2 2 3
出力例 1Copy
Copy
3 2 3
、
、
、
です。
入力例 2Copy
Copy
2 1 2
出力例 2Copy
Copy
1 1
入力例 3Copy
Copy
6 1 6 1 5 1 3 1 4 1 2
出力例 3Copy
Copy
5 9 9 9 9 9
Score : points
Problem Statement
Given is a tree with vertices. The vertices are numbered , and the -th edge is an undirected edge connecting Vertices and .
For each integer , find .
Here, denotes the minimum number of edges that must be traversed to go from Vertex to Vertex .
Constraints
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print lines.
The -th line should contain .
Sample Input 1Copy
Copy
3 1 2 2 3
Sample Output 1Copy
Copy
3 2 3
We have:
,
,
.
Sample Input 2Copy
Copy
2 1 2
Sample Output 2Copy
Copy
1 1
Sample Input 3Copy
Copy
6 1 6 1 5 1 3 1 4 1 2
Sample Output 3Copy
Copy
5 9 9 9 9 9