같은 팀 여부 확인
1. find 로직으로 각 학생의 루트노드 찾기
팀합치기 = union로직
1. a,b를 각 a,b의 루트노드로 변수 할당 (find로직)
2. if a<b → parent[a]=b
# 팀찾기 (루트노드 찾기)
def team_find(x,parent):
if parent[x] != x:
parent[x] = team_find(parent[x],parent)
return parent[x]
# 팀합치기
def team_union(a,b,parent):
a = team_find(a,parent)
b = team_find(b,parent)
if a<b:
parent[b]=a
else:
parent[a]=b
n,m = map(int,input().split())
parent=[0] * (n+1)
# 부모 리스트 자기자신으로 초기화
for i in range(n+1):
parent[i] = i
for i in range(m) :
k,a,b = map(int,input().split())
# 팀합치기
if k==0 :
team_union(a,b,parent)
# 팀 확인하기
else:
a = team_find(a,parent)
b = team_find(b,parent)
if a==b:
print("YES")
else:
print("NO")