Megalopolis
Time limit1sMemory limit128 MB
Count, for each query at a given moment, the number of still-country roads on the path from village 1 to a target village as edges are removed one by one.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Segment tree
- Solved
- No attempts yet
Problem
Byteotia has villages, numbered from to . Long ago they were joined by bidirectional dirt roads, called country roads, so that village (named Bitburg) could be reached from every other village in exactly one way. On that unique route from a village toward Bitburg, every village along the way had a number less than or equal to the village it started from, and each road joined exactly two distinct villages.
As time passed, the country roads were rebuilt into motorways one after another, until not a single dirt road remained. Byteasar the postman remembers the exact moment each road was rebuilt. He also recalls his deliveries: every trip began at Bitburg (village ) and ended at some village, and he now wonders how many country roads he walked along on each trip.
You are given the road network and a chronological list of events. Each event is either a road being turned into a motorway or one of Byteasar's trips. For every trip, report how many country roads lay on the path from village to the destination village at that moment.
Input
The first line contains an integer (), the number of villages.
Each of the next lines contains two integers and (), meaning that a country road joins villages and .
The next line contains an integer (), the number of trips.
The following lines describe the events in chronological order, one per line:
A a b(with ): the country road between villages and is turned into a motorway at this moment.W a: Byteasar makes a trip from Bitburg (village ) to village .
Output
Print exactly integers, one per line. The -th line is the number of country roads on the path from village to the destination of Byteasar's -th trip, counted at the moment that trip is made.
Hint
The figure below illustrates the tree of the first example.
