Regular Forestation
Time limit1sMemory limit512 MB
Given a tree, find a node whose removal splits it into two or more pairwise identical subtrees, and report the largest such number of pieces, or -1 if none exists.
- Level
Medium7 of 10
- Topics
- Tree, Hash map, Divide and conquer
- Solved
- No attempts yet
Problem
Forestation is the act of planting many trees to grow a forest, usually to replace a forest that has been cut down. Graph theorists have another idea: make a forest by cutting down a tree. A tree is a graph of nodes connected by edges. Let be a node in a tree whose degree is at least 2, that is, is directly connected to at least 2 other nodes in . If is removed from , two or more smaller disconnected trees remain, which graph theorists call a forest.
Let be the set of nodes in a tree and be the set of nodes in a tree . Trees and are identical if there exists a bijection such that for all pairs of nodes in , and are connected by an edge in if and only if and are connected by an edge in . If , then node in corresponds to node in .
A node in a tree is a good cutting point if removing from leaves two or more disconnected trees, and all of those trees are pairwise identical.
Given a tree , determine whether it has a good cutting point. If it does, output the maximum number of disconnected trees that can be obtained by removing exactly one good cutting point.
For example, consider the following tree of 13 nodes.

This tree has exactly one good cutting point, node 4. Removing node 4 gives three identical trees, which are line graphs. Trees A, B, and C are formed by the node sets , , and , respectively.
- A bijection between A and B: , , , .
- A bijection between A and C: , , , .
- A bijection between B and C: , , , .
Other bijections also exist.
Input
The first line contains an integer (), the number of nodes in the tree. Each of the next lines contains two integers and (), representing an edge of the tree. Any two nodes in the tree are connected by a sequence of edges.
Output
Print one integer on a line: the maximum number of disconnected trees that can be obtained by removing exactly one good cutting point. If there is no good cutting point, print .