#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:

  1. A tree of size nn consists of nn nodes and n1n - 1 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.
  2. For a tree of size nn and any node cc in the tree, cc is called a centroid of the tree if and only if, after deleting cc and all edges incident to it, the sizes of all resulting subtrees are no more than n2\lfloor \frac{n}{2} \rfloor (where x\lfloor x \rfloor 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 SS of size nn, with nodes numbered from 11 to nn. Xiaojiandan’s homework is to compute, for every edge deleted individually from SS, 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, EE denotes the edge set of tree SS, and (u,v)(u,v) denotes an edge connecting node uu and node vv. SuS'_u and SvS'_v denote, respectively, the two subtrees containing node uu and node vv after deleting edge (u,v)(u,v) from SS.

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 TT, indicating the number of test cases.

Then the input for each test case is given as follows:

The first line contains an integer nn, indicating the size of the tree SS.

The next n1n - 1 lines each contain two integers uiu_i and viv_i separated by a space, representing an edge (ui,vi)(u_i,v_i) in the tree.

Output Format

Output TT lines. Each line contains one integer. The integer on line ii means: for the tree given in test case ii, 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 (1,2)(1,2). The centroid labels of the subtree containing node 1 are {1}\{1\}. The centroid labels of the subtree containing node 2 are {2,3}\{2,3\}.

Delete edge (2,3)(2,3). The centroid labels of the subtree containing node 2 are {2}\{2\}. The centroid labels of the subtree containing node 3 are {3,5}\{3,5\}.

Delete edge (2,4)(2,4). The centroid labels of the subtree containing node 2 are {2,3}\{2,3\}. The centroid labels of the subtree containing node 4 are {4}\{4\}.

Delete edge (3,5)(3,5). The centroid labels of the subtree containing node 3 are {2}\{2\}. The centroid labels of the subtree containing node 5 are {5}\{5\}.

Therefore, the answer is 1+2+3+2+3+5+2+3+4+2+5=321 + 2 + 3 + 2 + 3 + 5 + 2 + 3 + 4 + 2 + 5 = 32.

[Constraints]

Test Point ID n=n = Special Property
121 \sim 2 77 None
353 \sim 5 199199
686 \sim 8 19991999
9119 \sim 11 4999149991 A
121512 \sim 15 262143262143 B
1616 9999599995 None
171817 \sim 18 199995199995
192019 \sim 20 299995299995

In the table, the meanings of the two special properties are that there exists a permutation pi(1in)p_i (1 \leq i \leq n) of 1n1 \sim n such that:

  • A: The tree is a chain. That is, 1i<n\forall 1 \leq i \lt n, there exists an edge (pi,pi+1)(p_i, p_{i + 1}).
  • B: The tree is a perfect binary tree. That is, 1in12\forall 1 \leq i \leq \frac{n-1}{2}, there exist two edges (pi,p2i)(p_i, p_{2i}) and (pi,p2i+1)(p_i, p_{2i+1}).

For all test points: 1T51 \leq T \leq 5, 1ui,vin1 \leq u_i,v_i \leq n. It is guaranteed that the given graph is a tree.

Translated by ChatGPT 5