#P2147. Cave Connectivity
Cave Connectivity
Cave Connectivity
Huihui is enthusiastic about cave exploration. One day, he arrives at a cave group area labeled JSZX based on a map. The area consists of \(n\) caves (numbered from 1 to n) and several channels. Each channel connects exactly two caves. Two caves are considered connected if there exists a sequence of channels linking them (this sequence is called a path connecting the caves).
The channels (or edges) are unstable and can appear or be destroyed over time. The monitoring instrument displays these changes in real time on the terminal as follows:
Connect u v
: A channel between caves \(u\) and \(v\) appears.Destroy u v
: The channel between caves \(u\) and \(v\) is destroyed.
It is guaranteed that at any moment there is at most one path between any two caves. Initially, there are no channels. In addition, the terminal can also receive the command:
Query u v
: Check whether cave \(u\) and cave \(v\) are currently connected.
Your task is to process a sequence of \(m\) commands. For every Query
command, output Yes
if the specified caves are connected; otherwise output No
.
inputFormat
The first line contains two integers \(n\) and \(m\) \((1 \le n, m \le 10^5)\), representing the number of caves and the number of commands, respectively.
The following \(m\) lines contain a command each, in one of the following formats:
Connect u v
Destroy u v
Query u v
Note: Commands are given online. Initially, there are no channels.
outputFormat
For each Query
command, output a single line: Yes
if the two caves are connected, or No
if they are not.
sample
5 7
Connect 1 2
Connect 2 3
Query 1 3
Query 1 4
Connect 4 5
Destroy 2 3
Query 1 3
Yes
No
No
</p>