We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4556fad commit 09f2645Copy full SHA for 09f2645
βπ G4/20040.py
@@ -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