Under construction

Still being built. Things may change

Similarity of Subtrees

Time limit2sMemory limit512 MB

Summary
For every node, count how many nodes share its depth profile in the rooted tree; sum the number of pairs with identical profiles.
Level

Medium7 of 10

Topics
Tree, DFS, Hash map, Sorting
Solved
No attempts yet

Problem

The depth of a node in a rooted tree is defined by applying the following rules recursively.

  • The depth of the root node is 0.
  • A child of a node with depth dd has depth d+1d+1.

Let S(T,d)S(T, d) be the number of nodes of TT with depth dd. Two rooted trees TT and T′T' are similar if and only if S(T,d)=S(T′,d)S(T, d) = S(T', d) for every non-negative integer dd.

You are given a rooted tree TT with NN nodes. The nodes of TT are numbered from 1 to NN, and node 1 is the root of TT. Let TiT_i be the rooted subtree of TT whose root is node ii. Write a program that counts the pairs (i,j)(i, j) such that TiT_i and TjT_j are similar and i<ji < j.

Input

The input consists of a single test case.

N
a1 b1
...
aN-1 bN-1

The first line contains an integer NN (1≤N≤100,0001 \le N \le 100{,}000), the number of nodes in the tree. The following N−1N-1 lines describe the branches: the ii-th of them contains aia_i and bib_i, meaning that node aia_i is the parent of node bib_i (1≤ai,bi≤N1 \le a_i, b_i \le N, ai≠bia_i \ne b_i). The root node is numbered 1. The given graph is guaranteed to be a rooted tree: every node except node 1 has exactly one parent, and the graph is connected.

Output

Print the number of pairs (x,y)(x, y) of nodes such that the subtree rooted at xx and the subtree rooted at yy are similar and x<yx < y.

Examples3

  1. Example 1

    Input
    5
    1 2
    1 3
    1 4
    1 5
    
    Expected output
    6
    
  2. Example 2

    Input
    6
    1 2
    2 3
    3 4
    1 5
    5 6
    
    Expected output
    2
    
  3. Example 3

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