#P73. [CSP-S 2019 Day 2] Subtree Centroids
[CSP-S 2019 Day 2] Subtree Centroids
Person in Charge
Problem Description
Little Jiandan is learning discrete mathematics, and today's content is the basics of graph theory. In class, he made the following two notes:
- A tree of size consists of nodes and undirected edges, and satisfies that there is exactly one simple path between any two nodes. If a node and its associated edges are removed from the tree, the tree will split into several subtrees; if an edge is removed from the tree (retaining the associated nodes, the same below), the tree will split into exactly two subtrees.
- For a tree of size and any node in the tree, is called the centroid of the tree if and only if after removing and its associated edges from the tree, the size of all split subtrees is no more than (where is the floor function). For a tree containing at least one node, it can have only 1 or 2 centroids.
After class, the teacher gave a tree of size , where the nodes are numbered from . Little Jiandan's homework is to find the sum of the sums of the centroid numbers of the two split subtrees after removing each edge of individually. That is:
$$\sum_{(u,v) \in E} \left( \sum_{1 \le x \le n \atop \text{and node } x \text{ is the centroid of } S'_u} x + \sum_{1 \le y \le n \atop \text{and node } y \text{ is the centroid of } S'_v} y \right)$$In the above formula, denotes the edge set of tree , and denotes an edge connecting node and node . and respectively represent the split subtrees containing node and node after removing edge from tree .
Little Jiandan finds the homework not easy at all, so he has to ask you for help. Please teach him how to solve it.
Input Format
This problem contains multiple test cases.
The first line contains an integer indicating the number of test cases.
Then the input data for each test case is given in sequence. For each test case:
The first line contains an integer indicating the size of tree .
The next lines each contain two integers and separated by a space, representing an edge in the tree.
Output Format
There are lines in total, each with an integer. The integer in the -th line represents: the sum of the sums of the centroid numbers of the two split subtrees after removing each edge individually from the tree given in the -th test case.
Samples
2
5
1 2
2 3
2 4
3 5
7
1 2
1 3
1 4
3 5
3 6
6 7
32
56
Sample 1 Explanation
For the first test case:
- Removing edge : the centroid number of the subtree containing node is , and the centroid numbers of the subtree containing node are .
- Removing edge : the centroid number of the subtree containing node is , and the centroid numbers of the subtree containing node are .
- Removing edge : the centroid numbers of the subtree containing node are , and the centroid number of the subtree containing node is .
- Removing edge : the centroid number of the subtree containing node is , and the centroid number of the subtree containing node is .
Therefore, the answer is .
Data Range
| Test Case ID | Special Property | |
|---|---|---|
| None | ||
| A | ||
| B | ||
| None | ||
In the "Special Property" column of the table, the meanings of the two properties are as follows: there exists a permutation of such that:
- A: The tree is a chain. That is, , there is an edge .
- B: The tree is a perfect binary tree. That is, , there are two edges and .
For of the data, . It is guaranteed that the given graph is a tree.