PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000186. Đếm nghịch thế khi thay đổi gốc cây (Tree Inversion)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm nghịch thế khi thay đổi gốc cây (Tree Inversion)

Tree Inversion

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given an undirected tree on vertices labeled 11 through NN, choose each vertex rr as a root in turn. Count ordered pairs (v,w)(v,w) such that (1) ww lies on the simple path from rr to vv (allowing either endpoint), and (2) the numeric labels satisfy the strict inequality v<wv<w. Let f(r)f(r) be the count. Compute f(1),f(2),…,f(N)f(1),f(2),\ldots,f(N). Equivalently, ww must be an ancestor of vv under root rr with v<wv<w; this is unrelated to DFS order inversions.

Input

The first line contains NN, followed by N−1N-1 undirected edges u v forming a tree.

Output

Print NN space-separated integers: f(1),…,f(N)f(1),\ldots,f(N).

Subtasks

  • Subtask 1 (20 points): 2≤N≤142\le N\le14; exactly N−1N-1 undirected edges with endpoints in [1,N][1,N] form a connected tree, with no loops or repeated edges.
  • Subtask 2 (30 points): 2≤N≤20002\le N\le2000; exactly N−1N-1 undirected edges with endpoints in [1,N][1,N] form a connected tree, with no loops or repeated edges.
  • Subtask 3 (50 points): 2≤N≤2000002\le N\le200000; exactly N−1N-1 undirected edges with endpoints in [1,N][1,N] form a connected tree, with no loops or repeated edges.

Examples

Example 1

Input

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

Output

0 1 3 4 8 9 15

Explanation

Root 1: 0 valid pairs v<wv<w with ww on the root-to-vv path; examples ∅. Root 2: 1 valid pairs v<wv<w with ww on the root-to-vv path; examples (1,2). Root 3: 3 valid pairs v<wv<w with ww on the root-to-vv path; examples (1,2), (1,3), (2,3). Root 4: 4 valid pairs v<wv<w with ww on the root-to-vv path; examples (1,2), (1,4), (2,4), (3,4). Root 5: 8 valid pairs v<wv<w with ww on the root-to-vv path; examples (1,2), (1,4), (1,5), (2,4), (2,5), (3,4), (3,5), (4,5). Root 6: 9 valid pairs v<wv<w with ww on the root-to-vv path; examples (1,2), (1,4), (1,6), (2,4), (2,6), (3,4), (3,6), (4,6), (5,6). Root 7: 15 valid pairs v<wv<w with ww on the root-to-vv path; examples (1,2), (1,4), (1,6), (1,7), (2,4), (2,6), (2,7), (3,4), (3,6), (3,7), (4,6), (4,7) ….

Example 2

Input

3
1 2
2 3

Output

0 1 3

Explanation

Root 1: 0 valid pairs v<wv<w with ww on the root-to-vv path; examples ∅. Root 2: 1 valid pairs v<wv<w with ww on the root-to-vv path; examples (1,2). Root 3: 3 valid pairs v<wv<w with ww on the root-to-vv path; examples (1,2), (1,3), (2,3).