ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 20040 |
문제명 | 사이클 게임 |
레벨 | 골드 4 |
분류 |
Disjoint set |
시간복잡도 | O(m*α(n)) |
인풋사이즈 | n<=500,000, m<=1,000,000 |
사용한 언어 | Python |
제출기록 | 38224KB / 1016ms |
최고기록 | 736ms |
해결날짜 | 2021/10/15 |
태그 |
"""Solution code for "BOJ 20040. 사이클 게임".
- Problem link: https://www.acmicpc.net/problem/20040
- Solution link: http://www.teferi.net/ps/problems/boj/20040
Tags: [DisjointSet]
"""
import sys
from teflib import disjointset
def main():
n, m = [int(x) for x in sys.stdin.readline().split()]
dsu = disjointset.DisjointSet(n)
for i in range(m):
a, b = [int(x) for x in sys.stdin.readline().split()]
try:
dsu.union(a, b, should_raise=True)
except ValueError:
print(i + 1)
break
else:
print('0')
if __name__ == '__main__':
main()