#P4582. [FJOI2014] 树的重心
[FJOI2014] 树的重心
Description
Given a tree with vertices, labeled , count how many distinct connected subtrees have the same centroid as the original tree.
A tree on vertices is a minimally connected graph on vertices. It obviously has edges. If you remove any one of these edges, the original graph becomes disconnected. Between any two vertices there is a unique path.
For a tree, the centroid is defined as follows: after deleting a vertex , if there are connected components left, define to be the maximum number of vertices among these components. A centroid is a vertex that minimizes .
Based on this definition, a tree may have one or two centroids. The connected subtrees required in this problem must have exactly the same centroid labels and the same number of centroids as the original tree.
Find how many connected subtrees in the given tree have the same centroid as the original tree. Output the result .
Input Format
The first line contains a positive integer , the number of test cases.
For each test case, first read an integer (), the number of vertices in the tree. Then read lines, each containing two integers (), indicating that there is an edge between vertices and . All vertices are labeled .
Output Format
First output the test case index, then output the number of connected subtrees that satisfy the condition. Since this number can be large, you only need to output this number . See the output example and strictly follow its format.
3
2
1 2
3
1 2
2 3
5
1 2
1 3
2 4
2 5
Case 1: 1
Case 2: 2
Case 3: 6
Hint
For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号