#P5666. [CSP-S 2019] 树的重心
[CSP-S 2019] 树的重心
Description
Xiaojiandan is learning discrete mathematics. Today’s topic is basic graph theory, and in class he wrote down the following two notes:
- A tree of size consists of nodes and undirected edges, and between any two nodes there exists exactly one simple path. If you delete one node in a tree and all edges incident to it, the tree will split into several subtrees. If you delete one edge in a tree (keeping its incident nodes; same below), the tree will split into exactly two subtrees.
- For a tree of size and any node in the tree, is called a centroid of the tree if and only if, after deleting and all edges incident to it, the sizes of all resulting subtrees are no more than (where is the floor function). For any tree with at least one node, it can have only 1 or 2 centroids.
After class, the teacher gave a tree of size , with nodes numbered from to . Xiaojiandan’s homework is to compute, for every edge deleted individually from , the sum of the centroid node labels of the two resulting subtrees. That is:
$$\sum_{(u,v) \in E} \left( \sum_{1 \leq x \leq n \atop 且 x 号点是 S'_u 的重心} x + \sum_{1 \leq y \leq n \atop 且 y 号点是 S'_v 的重心} y \right)$$In the expression above, denotes the edge set of tree , and denotes an edge connecting node and node . and denote, respectively, the two subtrees containing node and node after deleting edge from .
Xiaojiandan finds this homework not simple at all, so he asks you for help. Please teach him.
Input Format
This problem contains multiple test cases.
The first line contains an integer , indicating the number of test cases.
Then the input for each test case is given as follows:
The first line contains an integer , indicating the size of the tree .
The next lines each contain two integers and separated by a space, representing an edge in the tree.
Output Format
Output lines. Each line contains one integer. The integer on line means: for the tree given in test case , after deleting each edge individually, the sum of the centroid node labels of the two resulting subtrees.
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
Hint
[Sample 1 Explanation]
For the first test case:
Delete edge . The centroid labels of the subtree containing node 1 are . The centroid labels of the subtree containing node 2 are .
Delete edge . The centroid labels of the subtree containing node 2 are . The centroid labels of the subtree containing node 3 are .
Delete edge . The centroid labels of the subtree containing node 2 are . The centroid labels of the subtree containing node 4 are .
Delete edge . The centroid labels of the subtree containing node 3 are . The centroid labels of the subtree containing node 5 are .
Therefore, the answer is .
[Constraints]
| Test Point ID | Special Property | |
|---|---|---|
| None | ||
| A | ||
| B | ||
| None | ||
In the table, the meanings of the two special properties are that there exists a permutation of such that:
- A: The tree is a chain. That is, , there exists an edge .
- B: The tree is a perfect binary tree. That is, , there exist two edges and .
For all test points: , . It is guaranteed that the given graph is a tree.
Translated by ChatGPT 5
京公网安备 11011102002149号