Prime Tree
Time limit2sMemory limit512 MB
Given a rooted tree with n nodes, find the largest number of prime trees whose tree product equals it.
- Level
Hard8 of 10
- Topics
- Tree, Number theory, DFS
- Solved
- No attempts yet
Problem
Bobo proposes a multiplication operation on rooted trees.
Let and be two arbitrary rooted trees. Then is built by making a copy of for each vertex and merging the root of this copy with (see the following figure for more details).

Apparently, , where is the rooted tree with only one vertex. So, is a factor of every rooted tree, and every rooted tree is a factor of itself. If a rooted tree has only and as its factors, we call a prime tree.
Bobo has a rooted tree with nodes, labeled . He wants to factor into a product of as many prime trees as possible. That is, find an equation where each () is a prime tree and is maximum.
Note that is not a prime tree.
Input
The input contains zero or more test cases, and is terminated by end-of-file. For each test case:
The first line contains an integer , the number of nodes ().
The second line contains integers , where is the parent of the -th node ().
It is guaranteed that the sum of all does not exceed .
Output
For each test case, output an integer denoting the maximum number of prime factors.