AT_abc479_e.Cut and Add

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a tree with NN vertices numbered 1,2,…,N1,2,\dots,N. Here, NN is odd.
The edges are also numbered 1,2,…,N−11,2,\dots,N-1, and edge ii connects vertices AiA_i and BiB_i.

Each vertex ii has a variable xix_i, and initially xi=0x_i=0 for every vertex ii.

Now, for i=1,2,…,N−1i=1,2,\dots,N-1 in this order, perform the following operation.

  • First, remove edge ii. As a result, the tree is split into two connected components.
  • For every vertex vv in the connected component containing more vertices, add ii to xvx_v.
    • Since NN is odd, the connected component containing more vertices is uniquely determined.
  • Finally, restore edge ii.

Find the values of the variables x1,x2,…,xNx_1,x_2,\dots,x_N of the vertices after all operations are performed.

有一棵包含 NN 个顶点的树,顶点编号为 1,2,…,N1,2,\dots,N。其中 NN 为奇数。
树的边也编号为 1,2,…,N−11,2,\dots,N-1,第 ii 条边连接顶点 AiA_i 和 BiB_i。

每个顶点 ii 上有一个变量 xix_i,初始时对所有顶点 ii 均有 xi=0x_i = 0。

接下来,按 i=1,2,…,N−1i = 1,2,\dots,N-1 的顺序依次执行以下操作:

  • 首先,删除第 ii 条边。此时树被分割为两个连通分量。
  • 对于包含更多顶点的那个连通分量中的每个顶点 vv,将 ii 加到 xvx_v 上。
    • 由于 NN 是奇数,包含更多顶点的连通分量是唯一确定的。
  • 最后,恢复第 ii 条边。

在完成所有操作后,求各顶点的变量值 x1,x2,…,xNx_1,x_2,\dots,x_N。

输入格式

The input is given from Standard Input in the following format:

NN
A1A_1 B1B_1
A2A_2 B2B_2
⋮\vdots
AN−1A_{N-1} BN−1B_{N-1}

输入从标准输入中按以下格式给出:

NN
A1A_1 B1B_1
A2A_2 B2B_2
⋮\vdots
AN−1A_{N-1} BN−1B_{N-1}

输出格式

Output the final values of the variables in the following format:

x1x_1 x2x_2 …\dots xNx_N

以如下格式输出变量的最终值:

x1x_1 x2x_2 …\dots xNx_N

输入输出样例

  • 输入#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=1i=1, the following operation is performed.
    • Removing edge 11 splits the tree into a connected component with vertex 22 and a connected component with vertices 1,3,4,51,3,4,5.
    • The larger one is the connected component with vertices 1,3,4,51,3,4,5, so 11 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)(x_1,x_2,x_3,x_4,x_5)=(1,0,1,1,1).
    • Finally, edge 11 is restored.
  • For i=2i=2, the following operation is performed.
    • Removing edge 22 splits the tree into a connected component with vertex 55 and a connected component with vertices 1,2,3,41,2,3,4.
    • The larger one is the connected component with vertices 1,2,3,41,2,3,4, so 22 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)(x_1,x_2,x_3,x_4,x_5)=(3,2,3,3,1).
    • Finally, edge 22 is restored.
  • For i=3i=3, the following operation is performed.
    • Removing edge 33 splits the tree into a connected component with vertices 2,42,4 and a connected component with vertices 1,3,51,3,5.
    • The larger one is the connected component with vertices 1,3,51,3,5, so 33 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)(x_1,x_2,x_3,x_4,x_5)=(6,2,6,3,4).
    • Finally, edge 33 is restored.
  • For i=4i=4, the following operation is performed.
    • Removing edge 44 splits the tree into a connected component with vertex 33 and a connected component with vertices 1,2,4,51,2,4,5.
    • The larger one is the connected component with vertices 1,2,4,51,2,4,5, so 44 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)(x_1,x_2,x_3,x_4,x_5)=(10,6,6,7,8).
    • Finally, edge 44 is restored.

The final values of the variables are (x1,x2,x3,x4,x5)=(10,6,6,7,8)(x_1,x_2,x_3,x_4,x_5)=(10,6,6,7,8).

Constraints

  • All input values are integers.
  • NN is an odd number satisfying 3≤N≤2×105−13 \le N \le 2 \times 10^5 - 1.
  • 1≤Ai<Bi≤N1 \le A_i < B_i \le N
  • The given graph is a tree.

样例 1 解释:

  • 对于 i=1i=1,执行以下操作:
    • 删除边 11 将树分割为两个连通分量:一个包含顶点 22,另一个包含顶点 1,3,4,51,3,4,5。
    • 较大的连通分量是包含顶点 1,3,4,51,3,4,5 的那个,因此将 11 加到这些顶点对应的变量上。
    • 此时各变量的值变为 (x1,x2,x3,x4,x5)=(1,0,1,1,1)(x_1,x_2,x_3,x_4,x_5)=(1,0,1,1,1)。
    • 最后,恢复边 11。
  • 对于 i=2i=2,执行以下操作:
    • 删除边 22 将树分割为两个连通分量:一个包含顶点 55,另一个包含顶点 1,2,3,41,2,3,4。
    • 较大的连通分量是包含顶点 1,2,3,41,2,3,4 的那个,因此将 22 加到这些顶点对应的变量上。
    • 此时各变量的值变为 (x1,x2,x3,x4,x5)=(3,2,3,3,1)(x_1,x_2,x_3,x_4,x_5)=(3,2,3,3,1)。
    • 最后,恢复边 22。
  • 对于 i=3i=3,执行以下操作:
    • 删除边 33 将树分割为两个连通分量:一个包含顶点 2,42,4,另一个包含顶点 1,3,51,3,5。
    • 较大的连通分量是包含顶点 1,3,51,3,5 的那个,因此将 33 加到这些顶点对应的变量上。
    • 此时各变量的值变为 (x1,x2,x3,x4,x5)=(6,2,6,3,4)(x_1,x_2,x_3,x_4,x_5)=(6,2,6,3,4)。
    • 最后,恢复边 33。
  • 对于 i=4i=4,执行以下操作:
    • 删除边 44 将树分割为两个连通分量:一个包含顶点 33,另一个包含顶点 1,2,4,51,2,4,5。
    • 较大的连通分量是包含顶点 1,2,4,51,2,4,5 的那个,因此将 44 加到这些顶点对应的变量上。
    • 此时各变量的值变为 (x1,x2,x3,x4,x5)=(10,6,6,7,8)(x_1,x_2,x_3,x_4,x_5)=(10,6,6,7,8)。
    • 最后,恢复边 44。

最终各变量的值为 (x1,x2,x3,x4,x5)=(10,6,6,7,8)(x_1,x_2,x_3,x_4,x_5)=(10,6,6,7,8)。

约束条件

  • 所有输入值均为整数。
  • NN 是满足 3≤N≤2×105−13 \le N \le 2 \times 10^5 - 1 的奇数。
  • 1≤Ai<Bi≤N1 \le A_i < B_i \le N。
  • 给定图是一棵树。

输入解题思路,AI测评打分。不知道怎么写?

首页