It Takes a Village
Time limit20sMemory limit128 MB
Process online trading-post additions that spread through biconnected blocks and capital dominators, and answer revenue queries for single villages.
- Level
Hard9 of 10
- Topics
- Graph, DFS, Tree, Segment tree
- Solved
- No attempts yet
Problem
You are a sociologist studying a kingdom. The kingdom has a capital, several villages, and roads connecting them. Your research shows three conditions under which one village economically affects another. Village affects village if any of the following holds.
- There are two completely different paths from to that share no village other than and .
- Every path from to the capital passes through .
- affects some village , and affects .
The kingdom is building trading posts to lift the economy of its villages. Building a trading post in a village raises the revenue of that village, and it raises the revenue of every village that this village affects under the rules above. The king wants to know what his new trading posts are doing, so now and then he asks for the revenue of one village.
The king's actions are given in order, both building trading posts and asking about a village. Answer his questions.
Input
The input holds several test cases. Each test case begins with a line of three integers (), (), and (): the number of villages, the number of roads, and the number of actions the king performs. Villages are numbered from to , and village is the capital.
Each of the next lines holds two integers and (, ), a road between village and village . A road can be travelled in either direction. Two villages may be joined by more than one road, and in that case each road is a separate way to travel. Every village is reachable from the capital.
The next lines give the king's actions in order. An action has one of two forms.
+ k x
The king builds a trading post in village (), which raises the revenue of every affected village by ().
? k
The king asks for the total revenue of village (), counting the trading posts built in that village and the trading posts built in every village that affects it.
The parts of an action are separated by a single space, with no leading or trailing blanks. The input ends with a line holding three zeros.
Output
For each ? k action, print one integer on its own line, the answer to that question. Answer the questions in the order they are asked. Print no extra spaces and no blank lines.