Synchronization
Time limit8sMemory limit128 MB
Given a tree whose edges toggle on and off over time, report how many distinct information pieces each queried server holds at the end.
- Level
Hard8 of 10
- Topics
- Union-find, Divide and conquer, Graph, DFS
- Solved
- No attempts yet
Problem
A company runs servers around the world. Each server initially stores one unique piece of information: server stores information , and no two servers start with the same piece.
The company connects servers with communication lines so information can be shared. Whenever two servers become able to reach each other through the currently active lines, they synchronize: after synchronizing, every server in the same connected group holds the union of all pieces that any server in that group held. In other words, servers that are connected (directly or indirectly) always share exactly the same set of pieces.
To keep costs down, only lines are ever installed, and they are chosen so that, if all of them were active at the same time, the servers would form a single tree (there is a unique simple path between any two servers).
At time , no line is active. Because some lines run through harsh environments, a line may go down and later be rebuilt. At each time (for ) the state of exactly one line changes: if that line is currently inactive it becomes active, and if it is currently active it becomes inactive. All synchronization triggered by a change at time finishes before time .
Important: information is never lost. When an active line goes down and a group splits into two, each side keeps every piece it already held.
After all changes have been applied, report, for several chosen servers, how many distinct pieces of information that server holds.
Input
The input is given through standard input in the following format.
- The first line contains three integers , , and : the number of servers, the number of line-state changes, and the number of servers to query.
- Each of the next lines contains two integers and (): line , when active, connects server and server .
- Each of the next lines contains one integer (): at time the state of line is toggled.
- Each of the next lines contains one integer (): report the number of distinct pieces of information held by server after all changes.
Output
Print lines. The -th line must contain a single integer: the number of distinct pieces of information held by server after all changes have been applied.
Constraints
- .
- .
- .
- and for .
- for .
- for .
- All values are distinct.
- If every line is active at once, the servers are connected (the lines form a tree).
Example walkthrough
Consider the first sample, with servers. At the start server holds piece (for ).
- Time : line becomes active and connects servers and . Both now hold .
- Time : line becomes active and connects servers and . Together with line , servers , , are connected and all hold .
- Time : line goes down (it was active). Servers and can no longer reach each other, but each keeps .
- Time : line becomes active and connects servers and . Both now hold . They cannot reach server , because line is down.
- Time : line goes down. Servers and each keep .
- Time : line becomes active and connects servers and . Both now hold .
In the end servers , , and hold , , and distinct pieces respectively, which matches the first sample's output.