-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10775.py
42 lines (31 loc) · 803 Bytes
/
10775.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
36
37
38
39
40
41
42
import sys
from collections import deque
input = sys.stdin.readline
CLOSED = 0
def solution():
cnt = 0
while len(q_dock):
aircraft = q_dock.popleft()
a_root = find_root(aircraft-1)
b_root = find_root(aircraft)
if b_root == CLOSED:
break
union(a_root, b_root)
cnt += 1
print(cnt)
def union(a_root: int, b_root: int):
if b_root != a_root:
roots[b_root] = a_root
else:
roots[a_root] = roots[a_root-1]
def find_root(x: int):
if roots[x] == x:
return x
roots[x] = find_root(roots[x])
return roots[x]
if __name__ == '__main__':
g = int(input())
p = int(input())
q_dock = deque([int(input()) for i in range(p)])
roots = [gate for gate in range(g + 1)]
solution()