백준 1717 - 집합의 표현 (파이썬)
Updated:
Answer
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
n, m = map(int, input().split())
graph = [i for i in range(n + 1)]
def find(x):
if graph[x] == x:
return x
graph[x] = find(graph[x])
return graph[x]
def union(x, y):
x = find(x)
y = find(y)
if x == y:
return
if x > y:
graph[y] = x
else:
graph[x] = y
for _ in range(m):
a, b, c = map(int, input().split())
if a == 0:
union(b, c)
else:
if find(b) == find(c):
print('YES')
else:
print('NO')