문제: https://www.acmicpc.net/problem/1717
1717번: 집합의 표현
첫째 줄에 n(1≤n≤1,000,000), m(1≤m≤100,000)이 주어진다. m은 입력으로 주어지는 연산의 개수이다. 다음 m개의 줄에는 각각의 연산이 주어진다. 합집합은 0 a b의 형태로 입력이 주어진다. 이는 a가 ��
www.acmicpc.net
▶ 코드
n, m = map(int, input().split())
def getParent(parent, x):
if parent[x] == x: return x
return getParent(parent, parent[x])
def unionParent(parent, x, y):
x = getParent(parent, x)
y = getParent(parent, y)
if x < y:
parent[y] = x
else:
parent[x] = y
parent = [0]*(n+1)
for i in range(1,n+1):
parent[i] = i
for _ in range(m):
a, b, c = map(int, input().split())
if a == 0:
unionParent(parent, b, c)
else:
b = getParent(parent, b)
c = getParent(parent, c)
if b == c: print('YES')
else: print('NO')
'■코테 중요개념 > 유니온 파인드(Union Find)' 카테고리의 다른 글
[백준 11724] 연결 요소의 개수 (0) | 2020.05.30 |
---|---|
[개념] (0) | 2020.05.28 |