DeloitteOA #2
Count Balanced Nodes in a Tree
估计中等可在线判题
考点(AI 标注)树的遍历
You are given a tree rooted at node 1. A node is balanced if the subtrees of all its direct children have the same size; leaf nodes are considered balanced as stated in the problem. Output the total number of balanced nodes. This site supplements the parent-list input protocol and the size limits.
输入格式
The first line contains n (1≤n≤200000); the second line contains n−1 integers, the parent labels of nodes 2..n in order (1-based). When n=1, the second line may be empty. The input is guaranteed to form a tree rooted at node 1.
输出格式
Output the number of balanced nodes.
样例 1
输入:
1
输出:
1
样例 2
输入:
4
1 2 3
输出:
4
样例 3
输入:
4
1 1 2
输出:
3