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 09f2645 commit fab53e6Copy full SHA for fab53e6
βπ G5/7511.py
@@ -0,0 +1,31 @@
1
+import sys
2
+input = sys.stdin.readline
3
+t = int(input())
4
+
5
+def find(x):
6
+ if x == parent[x]: return x
7
+ parent[x] = find(parent[x])
8
+ return parent[x]
9
10
+def union(x, y):
11
+ x = find(x)
12
+ y = find(y)
13
+ if x != y:
14
+ parent[x] = y
15
16
+for i in range(t):
17
+ n = int(input())
18
+ k = int(input())
19
+ parent = [i for i in range(n)]
20
+ for _ in range(k):
21
+ a, b = map(int, input().split())
22
+ union(a, b)
23
+ m = int(input())
24
+ print(f'Scenario {i+1}:')
25
+ for _ in range(m):
26
+ u, v = map(int, input().split())
27
+ if find(u) == find(v):
28
+ print(1)
29
+ else:
30
+ print(0)
31
+ print()
0 commit comments