题目描述
Problem Description
Huihui is passionate about cave exploration.
One day, he followed the map to a region marked as JSZX, known for its cave clusters. After initial exploration, Huihui discovered that this area consists of n caves (numbered from 1 to n) and several passages, with each passage connecting exactly two caves. If two caves can be connected through one or more passages in a certain order, they are considered connected, and the sequence of passages forming this connection is called a path between these two caves. Caves are extremely sturdy and cannot be destroyed, but passages are unstable and often change due to external influences. For example, based on monitoring results from instruments, a passage sometimes appears between cave 123 and cave 127, and sometimes this passage is destroyed due to peculiar reasons.
Huihui has a monitoring instrument that displays each change in the passages on his terminal in real-time:
- If a passage appears between cave u and cave v, the terminal displays the command
Connect u v. - If a passage between cave u and cave v is destroyed, the terminal displays the command
Destroy u v.
After extensive manual calculations, Huihui noticed a peculiar phenomenon: no matter how the passages change, at any given time, there is at most one path between any two caves.
Believing this to be governed by some essential rule, Huihui spent day and night in front of the terminal, trying to study this rule through the changes in passages. However, one day, he collapsed under the piles of calculation papers... He smashed the terminal on the ground (which, being sturdy, was not damaged), and turned to you, saying, "You write the program for this."
Huihui wants to be able to issue the command Query u v at any time to ask the monitor if caves u and v are connected. Now, you need to write a program to answer each query. It is known that before the first command appears, there are no passages in the JSZX cave cluster.
Input Format
The first line contains two positive integers n and m, representing the number of caves and the number of commands that have appeared on the terminal, respectively. The following m lines represent the commands that have appeared on the terminal in sequence. Each line starts with a string s indicating the type of command ("Connect", "Destroy", or "Query", case-sensitive), followed by two integers u and v (1 ≤ u, v ≤ n) representing the cave numbers.
Output Format
For each Query command, output whether caves u and v are connected: output "Yes" if they are connected, otherwise output "No" (without quotes).
Sample Input and Output
Input Sample #1
200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127
Output Sample #1
No
Yes
No
Input Sample #2
3 5
Connect 1 2
Connect 3 1
Query 2 3
Destroy 1 3
Query 2 3
Output Sample #2
Yes
No
Notes
Data Description:
- 10% of the data satisfies n ≤ 1000, m ≤ 20000
- 20% of the data satisfies n ≤ 2000, m ≤ 40000
- 30% of the data satisfies n ≤ 3000, m ≤ 60000
- 40% of the data satisfies n ≤ 4000, m ≤ 80000
- 50% of the data satisfies n ≤ 5000, m ≤ 100000
- 60% of the data satisfies n ≤ 6000, m ≤ 120000
- 70% of the data satisfies n ≤ 7000, m ≤ 140000
- 80% of the data satisfies n ≤ 8000, m ≤ 160000
- 90% of the data satisfies n ≤ 9000, m ≤ 180000
- 100% of the data satisfies n ≤ 10000, m ≤ 200000
All Destroy commands will destroy an existing passage.
This problem has a large input and output size, so it is recommended for C/C++ users to use scanf and printf for I/O operations to avoid timeout.
@namespace_std added a set of Hack data on December 1, 2019.
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分