Editorial for COCI '15 Contest 1 #6 Uzastopni
Submitting an official solution before solving the problem yourself is a bannable offence.
Let  denote the set of all the possible joke intervals that can be found at the party if we observe person 
 and all their direct or indirect subordinates (the subtree of person 
).
Let us assume that for a person  we have calculated the values of sets 
 for each direct subordinate node to that person. Then we can calculate 
 using dynamic programming.
Let  denote the set of all right sides of the interval of jokes that can be heard in the subtree of person 
 and whose left side of the interval is equal to 
. We iterate in the descending order over all the possible left sides 
 and for each 
 we iterate over all the possible right sides 
 so that there is a child of node 
 that has the interval 
 in its set and we calculate the following relation:
A special case when , then simply means:
The time complexity of this algorithm is  where 
 is the number of different jokes, and 
 is the number of nodes. A bitset data structure can be used in order to accelerate calculating the union operation by 
 times.
Comments