Under construction

Still being built. Things may change

Regular Forestation

Time limit1sMemory limit512 MB

Summary
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 NN nodes connected by N−1N-1 edges. Let uu be a node in a tree UU whose degree is at least 2, that is, uu is directly connected to at least 2 other nodes in UU. If uu is removed from UU, two or more smaller disconnected trees remain, which graph theorists call a forest.

Let V(S)V(S) be the set of nodes in a tree SS and V(T)V(T) be the set of nodes in a tree TT. Trees SS and TT are identical if there exists a bijection f:V(S)→V(T)f: V(S) \to V(T) such that for all pairs of nodes (si,sj)(s_i, s_j) in V(S)V(S), sis_i and sjs_j are connected by an edge in SS if and only if f(si)f(s_i) and f(sj)f(s_j) are connected by an edge in TT. If f(s)=tf(s) = t, then node ss in SS corresponds to node tt in TT.

A node uu in a tree UU is a good cutting point if removing uu from UU leaves two or more disconnected trees, and all of those trees are pairwise identical.

Given a tree UU, 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 {5,1,7,13}\{5, 1, 7, 13\}, {8,2,11,6}\{8, 2, 11, 6\}, and {3,12,9,10}\{3, 12, 9, 10\}, respectively.

  • A bijection between A and B: f(5)=8f(5)=8, f(1)=2f(1)=2, f(7)=11f(7)=11, f(13)=6f(13)=6.
  • A bijection between A and C: f(5)=3f(5)=3, f(1)=12f(1)=12, f(7)=9f(7)=9, f(13)=10f(13)=10.
  • A bijection between B and C: f(8)=3f(8)=3, f(2)=12f(2)=12, f(11)=9f(11)=9, f(6)=10f(6)=10.

Other bijections also exist.

Input

The first line contains an integer NN (3≤N≤40003 \le N \le 4000), the number of nodes in the tree. Each of the next N−1N-1 lines contains two integers aia_i and bib_i (1≤ai<bi≤N1 \le a_i < b_i \le N), representing an edge (ai,bi)(a_i, b_i) 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 −1-1.

Examples2

  1. Example 1

    Input
    13
    1 5
    1 7
    2 4
    2 8
    2 11
    3 12
    4 7
    4 12
    6 11
    7 13
    9 10
    9 12
    
    Expected output
    3
    
  2. Example 2

    Input
    6
    1 2
    1 3
    2 4
    3 5
    3 6
    
    Expected output
    -1