Written by
nodejs-style
on
on
[백준] 20040번 : 사이클 게임 [파이썬]
[백준] 20040번 : 사이클 게임 [파이썬]
import sys
input = sys . stdin . readline
n , m = map ( int , input (). split ())
parent = list ( range ( n ))
def find ( x ):
if parent [ x ] != x :
parent [ x ] = find ( parent [ x ])
return parent [ x ]
def union ( a , b ):
if a < b :
parent [ b ] = a
else :
parent [ a ] = b
result = 0
for i in range ( m ):
a , b = map ( int , input (). split ())
a = find ( a )
b = find ( b )
if a == b :
result = i + 1
break
else :
union ( a , b )
from http://20210916start.tistory.com/176 by ccl(A) rewrite - 2021-11-19 01:00:43