#icpc2016autumne. [icpc2016autumn_e]Similarity of Subtrees
[icpc2016autumn_e]Similarity of Subtrees
MathJax.Hub.Config({ tex2jax: { inlineMath: [[""], ["\\(","\\)"]], processEscapes: true }}); blockquote { font-family: Menlo, Monaco, "Courier New", monospace; color: #333333; display: block; padding: 8.5px; margin: 0 0 9px; font-size: 12px; line-height: 18px; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; white-space: pre; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; }
Problem Statement
Define the depth of a node in a rooted tree by applying the following rules recursively:
- The depth of a root node is 0.
- The depths of child nodes whose parents are with depth are .
Let be the number of nodes of with depth . Two rooted trees and are similar if and only if equals for all non-negative integer .
You are given a rooted tree with nodes. The nodes of are numbered from 1 to . Node 1 is the root node of . Let be the rooted subtree of whose root is node . Your task is to write a program which calculates the number of pairs such that and are similar and .
Input
The input consists of a single test case.
…
The first line contains an integer (), which is the number of nodes in a tree. The following lines give information of branches: the -th line of them contains and , which indicates that a node is a parent of a node . (, ) The root node is numbered by 1. It is guaranteed that a given graph is a rooted tree, i.e. there is exactly one parent for each node except the node 1, and the graph is connected.
Output
Print the number of the pairs of the nodes such that the subtree with the root and the subtree with the root are similar and .
Sample Input 1
5
1 2
1 3
1 4
1 5```
### Output for the Sample Input 1
```plain
6```
* * *
### Sample Input 2
```plain
6
1 2
2 3
3 4
1 5
5 6```
### Output for the Sample Input 2
```plain
2```
* * *
### Sample Input 3
```plain
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```
### Output for the Sample Input 3
```plain
14```
* * *