Count distinct joke-type sets from root-connected guest groups with unique values where each subtree below a guest forms consecutive numbers.
Hard8Dynamic programmingTreeIntervalsNo attempts yetTime limit1sMemory limit32 MBPetar 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 N employees, Petar included, are numbered 1 to N, and person i tells jokes of type Vi. 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.
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, {3,1,2} and {5,1,2,4,3} are such sets.
Petar wants to know how many different sets of joke types he can see at his party under these rules.
The first line contains the integer N. (1≤N≤10000)
The second line contains N integers V1,V2,…,VN, where Vi is the type of jokes person i tells. (1≤Vi≤100)
Each of the following N−1 lines contains two integers A and B, meaning that person A is the direct supervisor of person B. (1≤A,B≤N)
Print the number of different sets of joke types that comply with the rules.
In the first example the party can show these sets of jokes: {2}, {2,3}, {2,3,4}, {1,2,3,4}, {1,2}, {1,2,3}.
In the second example the only possible sets are {3}, {3,4}, {3,4,5}. The person telling joke 6 cannot come to the party, because then the set of jokes {4,6} would not be a set of consecutive numbers.