Editorial for COCI '15 Contest 1 #6 Uzastopni


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Let S_x denote the set of all the possible joke intervals that can be found at the party if we observe person x and all their direct or indirect subordinates (the subtree of person x).

Let us assume that for a person x we have calculated the values of sets S for each direct subordinate node to that person. Then we can calculate S_x using dynamic programming.

Let Q_l denote the set of all right sides of the interval of jokes that can be heard in the subtree of person x and whose left side of the interval is equal to l. We iterate in the descending order over all the possible left sides l and for each l we iterate over all the possible right sides r so that there is a child of node x that has the interval [l, r] in its set and we calculate the following relation:

\displaystyle Q_l = Q_l \cup Q_{r+1}

A special case when l = v_x, then simply means:

\displaystyle Q_l = \{v_x\} \cup Q_{l+1}

The time complexity of this algorithm is \mathcal O(NK^3) where K is the number of different jokes, and N is the number of nodes. A bitset data structure can be used in order to accelerate calculating the union operation by 32 times.


Comments

There are no comments at the moment.