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

  1. A tree of size nn consists of nn nodes and n1n - 1 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.
  2. For a tree of size nn and any node cc in the tree, cc is called the centroid of the tree if and only if after removing cc and its associated edges from the tree, the size of all split subtrees is no more than n2\lfloor \frac{n}{2} \rfloor (where x\lfloor x \rfloor 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 SS of size nn, where the nodes are numbered from 1n1 \sim n. 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 SS 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, 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 respectively represent the split subtrees containing node uu and node vv after removing edge (u,v)(u,v) from tree SS.

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 TT 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 nn indicating the size of 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

There are TT lines in total, each with an integer. The integer in the ii-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 ii-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 (1,2)(1,2): the centroid number of the subtree containing node 11 is {1}\{1\}, and the centroid numbers of the subtree containing node 22 are {2,3}\{2,3\}.
  • Removing edge (2,3)(2,3): the centroid number of the subtree containing node 22 is {2}\{2\}, and the centroid numbers of the subtree containing node 33 are {3,5}\{3,5\}.
  • Removing edge (2,4)(2,4): the centroid numbers of the subtree containing node 22 are {2,3}\{2,3\}, and the centroid number of the subtree containing node 44 is {4}\{4\}.
  • Removing edge (3,5)(3,5): the centroid number of the subtree containing node 33 is {2}\{2\}, and the centroid number of the subtree containing node 55 is {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.

Data Range

Test Case 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 "Special Property" column of the table, the meanings of the two properties are as follows: there exists a permutation pi(1in)p_i (1 \le i \le n) of 1n1 \sim n such that:

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

For 100%100\% of the data, 1T5,1ui,vin1 \le T \le 5 , 1 \le u_i,v_i \le n. It is guaranteed that the given graph is a tree.