Veenus
InterviewTime limit1sMemory limit1024 MB
Given a rooted tree and a stream of toggles on node danger levels, report the lowest common ancestor of all currently high-danger nodes after each toggle.
- Level
Medium6 of 10
- Topics
- Tree, Sorting, Binary search
- Solved
- No attempts yet
Problem
Veenus has a hierarchical administrative division. The planet is split into regions, each region may be split into subregions, each subregion into sub-subregions, and so on. The planet has administrative units numbered to , where unit is the whole planet. So the administrative division of Veenus forms a tree rooted at .
Veenus has many volcanoes, and its residents worry constantly about possible eruptions. Each unit therefore has a volcanic activity response center. A center is activated when its unit declares a high danger level. A unit can have a high danger level while all of its subunits have low danger levels.
When several response centers are active at once, their work must be coordinated. The smallest unit that contains all units with a high danger level is put in charge of the situation. Each unit is considered strictly larger than all of its subunits, even when it has only one subunit.
Volcanic activity changes often. The program must receive reports that a unit's danger level changed from low to high or from high to low, and after each report it must find the unit that should now be in charge of the situation.
Input
The first line contains the number of administrative units ().
Each of the next lines describes one unit. Line starts with the number of subunits of unit (), followed by integers (). These mean that units are subunits of unit .
The next line contains the number of reports ().
Each of the next lines contains a report, given as integers and (, ). means that the danger level of unit changed from low to high, and means that it changed from high to low.
All units have a low danger level at the start, and the input is consistent. A report that a unit changed to high is only given when its level was low before, and vice versa.
Output
Print lines, each with one integer. The -th line is the number of the unit in charge after processing the report on line of the input. If all units have a low danger level, print .