Friends Two
View as PDFThere are people in a class, numbered
through
. Friendship is mutual: if
is friends
with
, then
is friends with
. Two people are in the same friend group if they are the same
person, or if they are connected by a chain of friendships (friends, friends of friends, and so on).
At the start of the year, some friendships exist. Over the year, those friendships break one by one
until none remain. You are given a chronological log of events. Every friendship that ever
existed appears in this log as a breakup, so the initial friendships are exactly the pairs that
later break.
Process the events in order and answer queries about who is still in the same friend group.
Input
The first line contains two integers and
, the number of people and the number of events.
The next lines each describe one event in one of the following forms:
1 a b— the friendship betweenand
breaks (and is removed). If that friendship is already gone, nothing happens.
2 a b— ask whetherand
are currently in the same friend group.
Output
For each event of type , output one line:
Yesifand
are in the same friend group,
Nootherwise.
Constraints
Example 1
Input
5 10
1 1 2
1 4 5
2 3 4
1 3 4
1 2 5
2 2 5
2 2 5
1 3 5
2 2 4
1 1 5
Output
Yes
No
No
No
Explanation
The breakups in the log are the pairs ,
,
,
,
, and
, so those six friendships exist at the start.
- After
and
break,
and
are still friends directly, so the answer is
Yes. - After
and
also break,
and
are no longer connected, so both queries answer
No. - After
breaks,
and
are still not connected, so the answer is
No.
Example 2
Input
10 20
2 3 9
2 6 10
1 3 10
2 4 8
1 1 9
1 2 8
2 6 10
2 4 8
1 2 9
1 6 10
1 5 8
1 2 8
1 5 6
2 7 9
1 8 10
2 7 9
2 2 4
1 3 5
1 5 8
2 1 8
Output
Yes
Yes
No
Yes
No
No
No
No
No
Comments