AT_abc479_e.Cut and Add
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a tree with N vertices numbered 1,2,…,N. Here, N is odd.
The edges are also numbered 1,2,…,N−1, and edge i connects vertices Ai and Bi.
Each vertex i has a variable xi, and initially xi=0 for every vertex i.
Now, for i=1,2,…,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 xv.
- Since N is odd, the connected component containing more vertices is uniquely determined.
- Finally, restore edge i.
Find the values of the variables x1,x2,…,xN of the vertices after all operations are performed.
有一棵包含 N 个顶点的树,顶点编号为 1,2,…,N。其中 N 为奇数。
树的边也编号为 1,2,…,N−1,第 i 条边连接顶点 Ai 和 Bi。
每个顶点 i 上有一个变量 xi,初始时对所有顶点 i 均有 xi=0。
接下来,按 i=1,2,…,N−1 的顺序依次执行以下操作:
- 首先,删除第 i 条边。此时树被分割为两个连通分量。
- 对于包含更多顶点的那个连通分量中的每个顶点 v,将 i 加到 xv 上。
- 由于 N 是奇数,包含更多顶点的连通分量是唯一确定的。
- 最后,恢复第 i 条边。
在完成所有操作后,求各顶点的变量值 x1,x2,…,xN。
输入格式
The input is given from Standard Input in the following format:
N
A1 B1
A2 B2
⋮
AN−1 BN−1
输入从标准输入中按以下格式给出:
N
A1 B1
A2 B2
⋮
AN−1 BN−1
输出格式
Output the final values of the variables in the following format:
x1 x2 … xN
以如下格式输出变量的最终值:
x1 x2 … xN
输入输出样例
输入#1
5 2 4 1 5 1 4 1 3
输出#1
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
说明/提示
Sample 1 Explanation:
- 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 (x1,x2,x3,x4,x5)=(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 (x1,x2,x3,x4,x5)=(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 (x1,x2,x3,x4,x5)=(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 (x1,x2,x3,x4,x5)=(10,6,6,7,8).
- Finally, edge 4 is restored.
The final values of the variables are (x1,x2,x3,x4,x5)=(10,6,6,7,8).
Constraints
- All input values are integers.
- N is an odd number satisfying 3≤N≤2×105−1.
- 1≤Ai<Bi≤N
- The given graph is a tree.
样例 1 解释:
- 对于 i=1,执行以下操作:
- 删除边 1 将树分割为两个连通分量:一个包含顶点 2,另一个包含顶点 1,3,4,5。
- 较大的连通分量是包含顶点 1,3,4,5 的那个,因此将 1 加到这些顶点对应的变量上。
- 此时各变量的值变为 (x1,x2,x3,x4,x5)=(1,0,1,1,1)。
- 最后,恢复边 1。
- 对于 i=2,执行以下操作:
- 删除边 2 将树分割为两个连通分量:一个包含顶点 5,另一个包含顶点 1,2,3,4。
- 较大的连通分量是包含顶点 1,2,3,4 的那个,因此将 2 加到这些顶点对应的变量上。
- 此时各变量的值变为 (x1,x2,x3,x4,x5)=(3,2,3,3,1)。
- 最后,恢复边 2。
- 对于 i=3,执行以下操作:
- 删除边 3 将树分割为两个连通分量:一个包含顶点 2,4,另一个包含顶点 1,3,5。
- 较大的连通分量是包含顶点 1,3,5 的那个,因此将 3 加到这些顶点对应的变量上。
- 此时各变量的值变为 (x1,x2,x3,x4,x5)=(6,2,6,3,4)。
- 最后,恢复边 3。
- 对于 i=4,执行以下操作:
- 删除边 4 将树分割为两个连通分量:一个包含顶点 3,另一个包含顶点 1,2,4,5。
- 较大的连通分量是包含顶点 1,2,4,5 的那个,因此将 4 加到这些顶点对应的变量上。
- 此时各变量的值变为 (x1,x2,x3,x4,x5)=(10,6,6,7,8)。
- 最后,恢复边 4。
最终各变量的值为 (x1,x2,x3,x4,x5)=(10,6,6,7,8)。
约束条件
- 所有输入值均为整数。
- N 是满足 3≤N≤2×105−1 的奇数。
- 1≤Ai<Bi≤N。
- 给定图是一棵树。
输入解题思路,AI测评打分。不知道怎么写?