#K88677. Friendship Queries

    ID: 37362 Type: Default 1000ms 256MiB

Friendship Queries

Friendship Queries

You are given n students numbered from 1 to n and a series of q queries that manipulate their friendship network.

Each query is one of the following types:

  • Type 1 u v: Establish a friendship between student u and student v.
  • Type 2 u v: Dissolve the friendship between student u and v. (Note: For the purpose of this problem, this operation will be ignored.)
  • Type 3: Check if all students are connected in a single connected component. Print YES if they are, or NO otherwise.

The friendship relation is transitive. That is, if u is friends with v and v is friends with w, then u and w are considered connected.

Use efficient union-find (disjoint set union) methods to solve this problem. All input is given through stdin and all output should be printed to stdout.

Note: The 'remove friendship' operation (Type 2) is not supported in union-find and should be safely ignored, as it does not affect the connectivity tests in the given test cases.

inputFormat

The first line contains two integers n and q denoting the number of students and the number of queries, respectively.

The following q lines each contain a query in one of the following formats:

  • 1 u v: Form a friendship between u and v.
  • 2 u v: Dissolve the friendship between u and v (ignored in processing).
  • 3: Check if all students are connected.

outputFormat

For each query of type 3, output a single line containing YES if all the students are in one connected component, or NO otherwise.

## sample
5 7
1 1 2
1 2 3
3
1 4 5
3
1 3 4
3
NO

NO YES

</p>