- Notifications
You must be signed in to change notification settings - Fork 846
/
Copy path2.py
35 lines (30 loc) · 1.04 KB
/
2.py
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
# 특정 원소가 속한 집합을 찾기
deffind_parent(parent, x):
# 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀적으로 호출
ifparent[x] !=x:
parent[x] =find_parent(parent, parent[x])
returnparent[x]
# 두 원소가 속한 집합을 합치기
defunion_parent(parent, a, b):
a=find_parent(parent, a)
b=find_parent(parent, b)
ifa<b:
parent[b] =a
else:
parent[a] =b
# 탑승구의 개수 입력받기
g=int(input())
# 비행기의 개수 입력받기
p=int(input())
parent= [0] * (g+1) # 부모 테이블 초기화
# 부모 테이블상에서, 부모를 자기 자신으로 초기화
foriinrange(1, g+1):
parent[i] =i
result=0
for_inrange(p):
data=find_parent(parent, int(input())) # 현재 비행기의 탑승구의 루트 확인
ifdata==0: # 현재 루트가 0이라면, 종료
break
union_parent(parent, data, data-1) # 그렇지 않다면 바로 왼쪽의 집합과 합치기
result+=1
print(result)