#abc337g. G - Tree Inversion

G - Tree Inversion

Score: 600600 points

问题描述

给定一棵包含 NN 个顶点的树 TT:顶点 11、顶点 22,…,顶点 NN。第 ii 条边(1i<N1\leq i < N)连接顶点 uiu_iviv_i

对于树 TT 中的顶点 uu,定义函数 f(u)f(u) 如下:

  • f(u)f(u)\coloneqq 满足以下两个条件的顶点 vvww 的对数:
    • 顶点 ww 在连接顶点 uuvv 的路径上。
    • v<wv < w

这里,当 u=wu=w 或者 v=wv=w 时,顶点 ww 也被认为是在连接顶点 uuvv 的路径上。

求解 f(1)f(1)f(2)f(2),…,f(N)f(N) 的值。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a tree TT with NN vertices: vertex 11, vertex 22, \ldots, vertex NN. The ii-th edge (1i<N)(1\leq i\lt N) connects vertices uiu_i and viv_i.

For a vertex uu in TT, define f(u)f(u) as follows:

  • f(u)f(u)\coloneqq The number of pairs of vertices vv and ww in TT that satisfy the following two conditions:
    • Vertex ww is contained in the path connecting vertices uu and vv.
    • v<wv\lt w.

Here, vertex ww is contained in the path connecting vertices uu and vv also when u=wu=w or v=wv=w.

Find the values of f(1),f(2),,f(N)f(1),f(2),\ldots,f(N).

Constraints

  • 2N2×1052\leq N\leq2\times10^5
  • 1uiN (1iN)1\leq u_i\leq N\ (1\leq i\leq N)
  • 1viN (1iN)1\leq v_i\leq N\ (1\leq i\leq N)
  • The given graph is a tree.
  • All input values are integers.

Input

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

NN

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uN1u_{N-1} vN1v_{N-1}

Output

Print the values of f(1),f(2),,f(N)f(1),f(2),\ldots,f(N) in this order, separated by spaces.

Sample Input 1

7
1 2
2 3
2 4
4 5
4 6
6 7

Sample Output 1

0 1 3 4 8 9 15

The given tree is as follows:

For example, f(4)=4f(4)=4. Indeed, for u=4u=4, four pairs (v,w)=(1,2),(1,4),(2,4),(3,4)(v,w)=(1,2),(1,4),(2,4),(3,4) satisfy the conditions.

Sample Input 2

15
14 9
9 1
1 6
6 12
12 2
2 15
15 4
4 11
11 13
13 3
3 8
8 10
10 7
7 5

Sample Output 2

36 29 32 29 48 37 45 37 44 42 33 36 35 57 35

The given tree is as follows:

The value of f(14)f(14) is 5757, which is the inversion number of the sequence (14,9,1,6,12,2,15,4,11,13,3,8,10,7,5)(14,9,1,6,12,2,15,4,11,13,3,8,10,7,5).

Sample Input 3

24
7 18
4 2
5 8
5 15
6 5
13 8
4 6
7 11
23 16
6 18
24 16
14 21
20 15
16 18
3 16
11 10
9 11
15 14
12 19
5 1
9 17
5 22
11 19

Sample Output 3

20 20 41 20 21 20 28 28 43 44 36 63 40 46 34 40 59 28 53 53 66 42 62 63

update @ 2024/3/10 01:28:42