Skip to content

Commit 09f2645

Browse files
committed
πŸ‘‘ G4-20040
1 parent 4556fad commit 09f2645

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

β€ŽπŸ‘‘ G4/20040.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
input = sys.stdin.readline
3+
sys.setrecursionlimit(10**6)
4+
5+
n, m = map(int, input().split())
6+
parent = [i for i in range(n)]
7+
def find(x):
8+
if x == parent[x]: return x
9+
parent[x] = find(parent[x])
10+
return parent[x]
11+
12+
def union(x, y):
13+
x = find(x)
14+
y = find(y)
15+
if x != y:
16+
parent[x] = y
17+
18+
for i in range(1, m+1):
19+
x, y = map(int, input().split())
20+
if find(x) == find(y):
21+
print(i)
22+
break
23+
union(x, y)
24+
else:
25+
print(0)

0 commit comments

Comments
Β (0)