Size of a Connected Component


Submit solution

Points: 2
Time limit: 2.0s
Memory limit: 256M

Problem type
Allowed languages
Python

Problem Description

Given an undirected graph with n nodes (numbered 1 to n), initially with no edges.

Now, perform m operations of the following three types:

  • C a b: Add an edge between node a and node b (a and b may be equal).
  • Q1 a b: Query whether node a and node b are in the same connected component (a and b may be equal).
  • Q2 a: Query the number of nodes in the connected component containing node a.

Input Format
The first line contains integers n and m.
The next m lines each contain an operation instruction: C a b, Q1 a b, or Q2 a.

Output Format
For each Q1 a b operation, output Yes if a and b are in the same connected component, otherwise output No.
For each Q2 a operation, output an integer representing the number of nodes in the connected component containing a.

Each result occupies one line.

Data Range
1 ≤ n, m ≤ 105

Input Sample:

5 5
C 1 2
Q1 1 2
Q2 1
C 2 5
Q2 5

Output Sample:

Yes
2
3

Comments

There are no comments at the moment.