Union of Sets
Problem Description
There are n elements numbered from 1 to n. Initially, each element is in its own set.
Now, m operations need to be performed. There are two types of operations:
- M a b: Merge the sets containing elements a and b. If they are already in the same set, ignore this operation.
- Q a b: Query whether elements a and b are in the same set.
Input Format
The first line contains integers n and m.
The next m lines each contain an operation instruction, either M a b or Q a b.
Output Format
For each query operation Q a b, output the result: if a and b are in the same set, output Yes; otherwise, output No.
Each result occupies one line.
Data Range
1 ≤ n, m ≤ 105
Input Sample:
4 5
M 1 2
M 3 4
Q 1 2
Q 1 3
Q 3 4
Output Sample:
Yes
No
Yes
Comments