-
Notifications
You must be signed in to change notification settings - Fork 0
/
1118.py
43 lines (40 loc) · 857 Bytes
/
1118.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
n=int(input())
father = [i for i in range(10010)]
forest = set()
isTree = {}
def findfather(x):
a = x
while x != father[x]:
x = father[x]
while a != father[a]:
z = a
a = father[a]
father[z] = x
return x
def Union(a,b):
fa = findfather(a)
fb = findfather(b)
if fa != fb:
father[fa] = fb
for _ in range(n):
_,*birds = map(int,input().split())
forest.add(birds[0])
for bird in birds[1:]:
Union(birds[0],bird)
forest.add(bird)
cnt = 0
for bird in forest:
fa = findfather(bird)
if isTree.get(fa) is None:
isTree[fa] = True
cnt += 1
print(cnt,len(forest))
k = int(input())
for _ in range(k):
a,b = map(int,input().split())
fa = findfather(a)
fb = findfather(b)
if fa != fb:
print("No")
else:
print("Yes")