Distributed Matrix
Time limit1sMemory limit1024 MB
Nodes join a tree rooted at node 1 while others fail and recover; for each query, sum the time since last recovery over both root paths if both endpoints are powered.
- Level
Medium7 of 10
- Topics
- Tree, Implementation
- Solved
- No attempts yet
Problem
The Matrix was modified to use as little energy as possible because of an energy shortage. There are Matrix nodes, numbered uniquely from to . Initially, only the generator, node , has energy.
Nodes that need power are connected one by one to nodes that already have power, and they receive energy from them. The connections form a power network shaped as a tree. Some nodes can fail and recover after some time. A node that is already in the network never reconnects to another node, even if a node that indirectly powers it has failed.
The unreliability of a node is the time elapsed since its last recovery. If the node has never failed, it is the time since it joined the network. The generator joins at time .
You must process events. Event happens exactly seconds after the start and is one of the following types:
!: node connects to node and starts receiving energy from it.-: node fails and stops passing energy on.+: node , which failed earlier, recovers and passes energy on again.?: find how reliable the pair of nodes and is.
For a query, check whether both nodes receive energy. A node receives energy if it is connected to the network and every node on the path from the generator to it, including itself, is working. If both nodes receive energy, output the total unreliability of all nodes that the work of at least one of or depends on, that is, the nodes on the paths from the generator to them. A node shared by both paths is counted once.
Input
The first line contains two integers and (), the number of nodes that need power and the number of events to process.
The -th of the next lines describes the query that happens at time . The query type symbol is followed by either one integer or two integers and , separated by a space, depending on the type (; ).
It is guaranteed that in a type 1 query, node is already connected to the network and node is not. For type 2 and type 3 queries, node fails only if it was working before, and it recovers only after the corresponding failure.
Output
After each type 4 query, print the answer on its own line. If at least one of and does not receive energy, print -1. Otherwise, print the sum of the unreliabilities of all nodes on the paths from the generator to and .
Hint
In the first example, the failure of the third node makes the answer to the second query ? 2 3 equal to -1. The answer to the first query is , because , , and seconds have passed since the generator, the second node, and the third node joined the network, respectively. At the third query, and seconds have passed since the generator and the second node joined. The third node recovered exactly second ago, giving the answer .
In the second example, the failure of the second node makes the answer to the second query -1. The answer to the first query is calculated as in the first example. The answer to the third query is .