ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 10775 |
문제명 | 공항 |
레벨 | 골드 2 |
분류 |
Disjoint Set |
시간복잡도 | O(m+n*α(m)) |
인풋사이즈 | n<=10^5, m<=10^5 |
사용한 언어 | Python |
제출기록 | 39356KB / 288ms |
최고기록 | 184ms |
해결날짜 | 2022/01/11 |
"""Solution code for "BOJ 10775. 공항".
- Problem link: https://www.acmicpc.net/problem/10775
- Solution link: http://www.teferi.net/ps/problems/boj/10775
Tags: [Disjoint Set]
"""
import sys
from teflib import disjointset
def main():
G = int(sys.stdin.readline())
P = int(sys.stdin.readline())
g = [int(sys.stdin.readline()) for _ in range(P)]
dsu = disjointset.DisjointSet(G)
gate_by_set = list(range(G))
for i, g_i in enumerate(g):
set_id = dsu.find(g_i - 1)
gate = gate_by_set[set_id]
if gate is None:
answer = i
break
if gate == 0:
gate_by_set[set_id] = None
else:
prev_gate = gate_by_set[dsu.find(gate - 1)]
gate_by_set[dsu.union(gate, gate - 1)] = prev_gate
else:
answer = P
print(answer)
if __name__ == '__main__':
main()