백준 1717 집합의 표현 - 파이썬 풀이 🦄
문제
- DP 문제
Keypoints
- 넣는 순서가 아닌 값(value)에 대해서, 개수를 추적한다.
Solution
n, m = map(int, input().split())
parent = [i for i in range(n+1)]
def find(x):
if parent[x]==x:
return parent[x]
else:
parent[x] = find(parent[x])
return parent[x]
def union(x,y):
x, y = find(x), find(y)
if x < y :
parent[y] = x
elif x > y:
parent[x] = y
import sys
sys.setrecursionlimit(1000000)
for i in range(m):
op, a,b = map(int, sys.stdin.readline().strip().split())
if op==1:
if find(a)==find(b):
print("YES")
else:
print("NO")
else:
union(a,b)
References
'프로그래밍' 카테고리의 다른 글
[Error 🦗] ModuleNotFoundError: No module named 'tree' (0) | 2021.04.13 |
---|---|
백준 2608 로마숫자 - 파이썬 풀이 🦄 (0) | 2021.04.12 |
백준 1965 상자넣기 - 파이썬 풀이 🦄 (0) | 2021.03.31 |
백준 15992 1,2,3 더하기 7 - 파이썬 풀이 🦄 (0) | 2021.03.31 |
백준 17144 미세먼지 안녕 - 파이썬 풀이 🦄 (0) | 2021.03.31 |