목차

사이클 게임

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
태그

29단계

풀이

코드

"""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()