Party joke sets
Time limit1sMemory limit32 MB
Count distinct joke-type sets from root-connected guest groups with unique values where each subtree below a guest forms consecutive numbers.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Tree, Intervals
- Solved
- No attempts yet
Problem
Petar is throwing a birthday party and wants to invite some of the employees of the company he runs. Petar is the CEO, and since the party is his, he always attends.
The employees, Petar included, are numbered 1 to , and person tells jokes of type . Every employee except Petar has exactly one direct supervisor.
Petar is the CEO, so he has number 1 and is the direct or indirect supervisor of every employee.
Everyone at the party, Petar included, must follow these rules.
- No two people at the party tell the same type of jokes.
- Person cannot be invited if the direct supervisor of is not invited.
- Person cannot be invited if the joke types told by together with the joke types told by the invited people that supervises, directly or indirectly, do not form a set of consecutive numbers.
A set is a set of consecutive numbers when, after sorting it in ascending order, the difference between adjacent elements is exactly 1. For example, and are such sets.
Petar wants to know how many different sets of joke types he can see at his party under these rules.
Input
The first line contains the integer . ()
The second line contains integers , where is the type of jokes person tells. ()
Each of the following lines contains two integers and , meaning that person is the direct supervisor of person . ()
Output
Print the number of different sets of joke types that comply with the rules.
Note
In the first example the party can show these sets of jokes: , , , , , .
In the second example the only possible sets are , , . The person telling joke 6 cannot come to the party, because then the set of jokes would not be a set of consecutive numbers.