#FT0000186. Đếm nghịch thế khi thay đổi gốc cây (Tree Inversion)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đế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 through , choose each vertex as a root in turn. Count ordered pairs such that (1) lies on the simple path from to (allowing either endpoint), and (2) the numeric labels satisfy the strict inequality . Let be the count. Compute . Equivalently, must be an ancestor of under root with ; this is unrelated to DFS order inversions.
Input
The first line contains , followed by undirected edges u v forming a tree.
Output
Print space-separated integers: .
Subtasks
- Subtask 1 (20 points): ; exactly undirected edges with endpoints in form a connected tree, with no loops or repeated edges.
- Subtask 2 (30 points): ; exactly undirected edges with endpoints in form a connected tree, with no loops or repeated edges.
- Subtask 3 (50 points): ; exactly undirected edges with endpoints in 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 with on the root-to- path; examples ∅. Root 2: 1 valid pairs with on the root-to- path; examples (1,2). Root 3: 3 valid pairs with on the root-to- path; examples (1,2), (1,3), (2,3). Root 4: 4 valid pairs with on the root-to- path; examples (1,2), (1,4), (2,4), (3,4). Root 5: 8 valid pairs with on the root-to- path; examples (1,2), (1,4), (1,5), (2,4), (2,5), (3,4), (3,5), (4,5). Root 6: 9 valid pairs with on the root-to- 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 with on the root-to- 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 with on the root-to- path; examples ∅. Root 2: 1 valid pairs with on the root-to- path; examples (1,2). Root 3: 3 valid pairs with on the root-to- path; examples (1,2), (1,3), (2,3).