-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy path1118. Birds in Forest (25) .cpp
58 lines (58 loc) · 1.34 KB
/
1118. Birds in Forest (25) .cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;
int n, m, k;
const int maxn = 10010;
int fa[maxn] = {0}, cnt[maxn] = {0};
int findFather(int x) {
int a = x;
while(x != fa[x])
x = fa[x];
while(a != fa[a]) {
int z = a;
a = fa[a];
fa[z] = x;
}
return x;
}
void Union(int a, int b) {
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB) fa[faA] = faB;
}
bool exist[maxn];
int main() {
scanf("%d", &n);
for(int i = 1; i <= maxn; i++)
fa[i] = i;
int id, temp;
for(int i = 0; i < n; i++) {
scanf("%d%d", &k, &id);
exist[id] = true;
for(int j = 0; j < k-1; j++) {
scanf("%d", &temp);
Union(id, temp);
exist[temp] = true;
}
}
for(int i = 1; i <= maxn; i++) {
if(exist[i] == true) {
int root = findFather(i);
cnt[root]++;
}
}
int numTrees = 0, numBirds = 0;
for(int i = 1; i <= maxn; i++) {
if(exist[i] == true && cnt[i] != 0) {
numTrees++;
numBirds += cnt[i];
}
}
printf("%d %d\n", numTrees, numBirds);
scanf("%d", &m);
int ida, idb;
for(int i = 0; i < m; i++) {
scanf("%d%d", &ida, &idb);
printf("%s\n", (findFather(ida) == findFather(idb)) ? "Yes" : "No");
}
return 0;
}