-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathA1118.cpp
64 lines (60 loc) · 1.08 KB
/
A1118.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
59
60
61
62
63
64
#include <cstdio>
using namespace std;
const int MAXN = 10000 + 10;
int N, Q, birdNum = 0, cnt = 0, father[MAXN];
bool isRoot[MAXN] = {false};
int findFather(int x){
int a = x;
while(x != father[x]){
x = father[x];
}
while(a != father[a]){
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b){
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB){
father[faA] = faB;
}
}
int main(){
for(int i = 0; i < MAXN; ++i){
father[i] = i;
}
scanf("%d", &N);
for(int i = 0; i < N; ++i){
int k;
scanf("%d", &k);
if(k > 0){
int f, x;
scanf("%d", &f);
if(f > birdNum) birdNum = f;
for(int j = 0; j < k-1; ++j){
scanf("%d", &x);
if(x > birdNum) birdNum = x;
Union(x, f);
}
}
}
for(int i = 1; i <= birdNum; ++i){
isRoot[findFather(i)] = true;
}
for(int i = 1; i <= birdNum; ++i){
if(isRoot[i]){
++cnt;
}
}
printf("%d %d\n", cnt, birdNum);
scanf("%d", &Q);
for(int i = 0; i < Q; ++i){
int a, b;
scanf("%d%d", &a, &b);
printf("%s\n", findFather(a) == findFather(b) ? "Yes" : "No");
}
return 0;
}