#P2147. [SDOI2008] 洞穴勘测
[SDOI2008] 洞穴勘测
Description
Huihui is enthusiastic about cave exploration.
One day, following a map, he arrived at an area marked as JSZX, which is a cluster of caves. After a preliminary survey, Huihui found that this area consists of caves (numbered from to ) and several tunnels, where each tunnel connects exactly two caves. If two caves can be connected in order by one or more tunnels, then these two caves are said to be connected, and the sequence of tunnels connected in order is called a path between the two caves. The caves are very solid and cannot be destroyed, but the tunnels are unstable and often change due to external influences. For example, according to the monitoring device, sometimes a tunnel appears between cave and cave , and sometimes this tunnel is destroyed for some odd reason.
Huihui has a monitoring device that can display every change to the tunnels on the terminal at his side:
- If a tunnel appears between cave and cave (guaranteed that it did not previously exist), the terminal displays the instruction
Connect u v. - If the tunnel between cave and cave is destroyed (guaranteed that it previously existed), the terminal displays the instruction
Destroy u v.
After long and arduous manual calculations, Huihui discovered a strange phenomenon: no matter how the tunnels change, at any time there is at most one path between any two caves.
Therefore, he is convinced that this is governed by some essential rule. He kept working day and night in front of the terminal, trying to study this rule through the changes in the tunnels. However, one day he finally collapsed among the piles of scratch paper... He smashed the terminal onto the ground (the terminal is also solid and cannot be destroyed) and turned to you for help, saying: “Please write this program for me.”
Huihui wants to be able to issue the instruction Query u v at any time to ask the monitor whether cave and cave are currently connected. Now you need to write a program to answer each query. It is known that before the first instruction appears, there are no tunnels in the JSZX cave cluster.
Input Format
The first line contains two positive integers and , representing the number of caves and the number of instructions shown on the terminal.
The following lines describe the instructions that appear on the terminal in order:
- Each line begins with a string indicating the type of instruction.
- Then there are two integers , representing the indices of the two caves.
Output Format
For each Query instruction, output whether cave and cave are connected: output Yes if connected, otherwise output No.
200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127
No
Yes
No
3 5
Connect 1 2
Connect 3 1
Query 2 3
Destroy 1 3
Query 2 3
Yes
No
Hint
Constraints:
- For of the testdata, , .
- For of the testdata, , , , and all instructions are valid.
The I/O scale is large. It is recommended that C/C++ users use scanf and printf for I/O to avoid timeouts.
@namespace_std added a set of hack testdata on 2019.12.1.
Translated by ChatGPT 5
京公网安备 11011102002149号