-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10-2.py
More file actions
38 lines (31 loc) · 764 Bytes
/
10-2.py
File metadata and controls
38 lines (31 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
@file 10-2.py
@brief 팀 결성
@desc 서로소 집합 알고리즘
"""
# 팀 합치기
def union_parent(parent, a, b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b:
parent[b] = a
else:
parent[a] = b
# 같은 팀 여부 확인
def find_parent(parent, x):
if parent[x] != x:
parent[x] = find_parent(parent, parent[x])
return parent[x]
n, m = map(int, input().split())
parent = [0] * (n + 1)
for i in range(0, n + 1):
parent[i] = i
for i in range(m):
op, a, b = map(int, input().split())
if op == 0:
union_parent(parent, a, b)
elif op == 1:
if find_parent(parent, a) == find_parent(parent, b):
print("YES")
else:
print("NO")