Skip to content

Commit

Permalink
[코딩테스트책] 10-1. 팀 결성 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
heerucan committed Mar 27, 2022
1 parent 3f9bd36 commit a36c250
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions CodingTest/CH10 그래프 이론/팀결성.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 같은 팀 여부 확인
def find_parent(parent, x):
if parent[x] != x:
parent[x] = find_parent(parent, parent[x])
return parent[x]

# 팀 합치기
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

# n번, m개 연산
n, m = map(int, input().split())
parent = [0]*(n+1) #부모 테이블 초기화

# 부모 테이블 상에서, 부모를 자기 자신으로 초기화
for i in range(1, n+1):
parent[i] = i

# union 연산을 각각 수행
for i in range(m):
cal, a, b = map(int, input().split())
if cal == 0:
union_parent(parent, a, b)
else:
if find_parent(parent, a) == find_parent(parent, b):
print("YES")
else:
print("NO")

0 comments on commit a36c250

Please sign in to comment.