QOJ.ac

QOJ

时间限制: 1 s 内存限制: 512 MB 总分: 100

#16486. Pair Counting

统计

Given an unrooted tree with $n$ nodes.

Initially, you need to choose a node as the root; subsequently, you can perform any number of operations.

In each operation, you must perform the following steps in order:

  • Choose a positive integer $k$.
  • Mark every node in the current trees that is at distance $k$ from its root, and remove the edges connecting these nodes to their parents, forming several new trees.
  • Let these marked nodes be the roots of the corresponding new trees.

Here, the distance between node $u$ and node $v$ is equal to the number of edges on the simple path between them.

For a pair of nodes $(x, y)$, if there exists a choice of root and a sequence of operations such that nodes $x$ and $y$ are marked in the same operation, we call this pair beautiful.

You need to find the number of beautiful pairs $(x, y)$ such that $1 \le x < y \le n$.

Input

This problem contains multiple test cases.

The first line contains an integer $T$ representing the number of test cases ($1 \le T \le 10^5$).

For each test case:

  • The first line contains an integer $n$ ($3 \le n \le 10^6$).
  • The next $n-1$ lines each contain two integers $u_i, v_i$, representing an edge connecting nodes $u_i$ and $v_i$.

It is guaranteed that the given edges form a tree, and the sum of $n$ over all test cases does not exceed $10^6$.

Output

For each test case, output a single integer representing the answer.

Examples

Input 1

6
3
2 1
3 2
4
1 2
3 2
2 4
7
7 3
6 5
3 1
1 2
1 5
5 4
9
4 5
5 9
2 8
5 1
3 8
8 9
8 6
6 7
10
3 8
3 2
9 7
4 3
6 1
5 2
3 6
3 7
6 10
10
4 6
3 7
10 8
3 5
2 8
6 10
1 2
5 9
8 9

Output 1

1
3
12
26
28
36

Note

For the first test case, the only beautiful pair is $(1, 3)$.

For the second test case, the beautiful pairs are $(1, 3)$, $(1, 4)$, and $(3, 4)$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.