Cuckoos
Time limit2sMemory limit1024 MB
Each nest holds at most one egg, so the eggs form a graph; answer whether an insertion terminates, add eggs when it does, and count the ordered pairs that terminate.
- Level
Hard10 of 10
- Topics
- Graph, Union-find
- Solved
- No attempts yet
Problem
British scientists decided to study ornithology and investigate the life of an unusual kind of cuckoo. They planted a large tree and put nests on it, each inhabited by one cuckoo. The investigation checks whether an extra egg can be put into a given nest.
Each egg can be raised in only two of the nests. An egg is defined by a pair of distinct integers . The egg may be raised in nest or nest and in no other nest. The egg is not the same as the egg .
Putting an egg into nest works as follows. If nest is empty, stays there and the process ends. Otherwise, if nest already holds an egg , the cuckoo puts into nest and tries to put the displaced egg into nest in the same way. The process continues.
Answer the scientists' queries. There are three types.
- (Theoretical) Will putting an egg into nest eventually stop? This egg is not added, and the state of the nests does not change.
- (Practical) Will putting an egg into nest eventually stop? If it does, the egg is added following the process above.
- (Theoretical) How many ordered pairs of distinct integers exist such that the egg can be put into nest and the process eventually stops? Each pair is judged independently of the others. No egg is added by this query.
Input
The first line contains three integers , , (, , ), where is the number of nests, is the number of eggs already placed, and is the number of queries.
Each of the next lines contains two integers and , meaning nest contains the egg . All are distinct, and for all .
The next lines are the queries, in the order they must be processed. The first number is the query type. For or , two distinct integers and follow, describing the egg in that query. A query of type 1 does not add the egg. A query of type 2 adds the egg if the process requires a finite number of egg transfers. For , the query asks for the count described above, and no egg is added.
Output
For each query of type 1 or type 2, print Yes if the process is finite and No otherwise. For each query of type 3, print the number of ordered pairs.
Constraints
Let denote the number of queries of type 1, the number of queries of type 2, and the number of queries of type 3.
Hint
The initial state of the nests in the sample is as follows. Nest 1 contains the egg , nest 2 contains the egg , nest 5 contains the egg , and nests 3 and 4 are empty.
The egg can be added even though the same egg is already present. This transfers the existing egg to the remaining nest.
In the initial state, any of the 10 possible eggs for a tree with five nests can be added, and each egg can go into either of its two nests, so the process is finite. The answer to the second query is 20.
After the next query adds the egg , nest 1 contains an egg , nest 2 contains an egg , nest 4 contains the egg , and nest 5 contains the egg . Now only the eggs , , , and can be added. Each of them can still go into either of its corresponding nests, so the answer is 8.
The egg cannot be added in a finite number of steps, so the state of the nests does not change.
Adding the egg requires 5 egg transfers, and after that no new egg can be added in a finite number of steps.