The country has $n$ cities and $n − 1$ bidirectional roads

813 Views Asked by At

I don't understand how to compute output in the examples. If problem is easy, sorry.

From eolymp:

The country has $n$ cities and $n − 1$ bidirectional roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from $1$ to $n$ inclusive.

All the roads are initially bad, but the government wants to improve the state of some roads. We will assume that the citizens are happy about road improvements if the path from the capital, located in city $1$, to any other city contains at most one bad road.

Determine the number of ways to improve the quality of some roads in order to meet the citizens' needs. As the answer can be large, print it modulo $1 000 000 007$ ($10^9$ + $7$)

Input data

The first line contains a single integer ($2 ≤ n ≤ $ $2*10^5$) — the number of cities in the country. The next line contains $ n − 1 $ positive integers $p_2, p_3, p_4, \dots, p_n$ $( 1 \leq p_i \leq i - 1 )$ — the description of the roads in the country. The number $p_i$ means that the country has a road connecting city $p_i$ and city $I$.

Output data

Print the number of ways to improve the quality of the roads modulo $1 000 000 007$ ($10^9$ + $7$)


Examples:

Input example #$1$:

3
1 1

Output example #$1$:

4

Input example #$2$:

6
1  2  2  1  5

Output example #$2$:

15
1

There are 1 best solutions below

6
On BEST ANSWER

I will spell out example #1, I think if you understand the notation you can do example 2 yourself.

First notice that the criteria of $n$ cities $n-1$ roads and all cities can be reached via road together imply that the road graph is in fact a tree. So there are no cycles and for any two cities there is exactly one path along the roads connecting them.

In example #1 there are three cities and $p_2=1$, $p_3=1$ means the second and third city are both connected to city 1. Now any program of road improvement may or may not improve each individual road. With 2 roads this gives a total of $2^2$ possible programs.

Now you need to check for each program whether the condition 'route from any city to city 1 contains at most one unimproved road' is satisfied. In example #1 both the route from city 2 to city 1 and the route from city 3 to city 1 only contain one road in total, so the criteria 'at most one unimproved' is automatically fulfilled regardless of which roads are improved. Hence the correct output contains all 4 possible improvement programs.