#K88677. Friendship Queries
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 studentv
. - Type 2 u v: Dissolve the friendship between student
u
andv
. (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, orNO
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 betweenu
andv
.2 u v
: Dissolve the friendship betweenu
andv
(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.
5 7
1 1 2
1 2 3
3
1 4 5
3
1 3 4
3
NO
NO
YES
</p>