#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10010;
int isRoot[maxn] = {0}, rec[maxn] = {0}, father[maxn];
int find(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 r1 = find(a), r2 = find(b);
if(r1 != r2) father[r2] = r1;
}
int main(){
int n, k, n1, n2;
cin >> n;
for(int i = 1; i <= maxn; i++) father[i] = i;
for(int i = 0; i < n; i++){
cin >> k >> n1; rec[n1] = 1;
for(int j = 0; j < k-1; j++){
cin >> n2;
Union(n1, n2); rec[n2] = 1;
}
}
int ans = 0, cnt = 0;
for(int i = 1; i < maxn; i++) if(rec[i] > 0) cnt++;
for(int i = 1; i <= cnt; i++) isRoot[find(i)] = 1;
for(int i = 1; i <= cnt; i++) ans += isRoot[i];
printf("%d %d\n", ans, cnt);
int q, a, b;
cin >> q;
for(int i = 0; i < q; i++){
cin >> a >> b;
if(father[a] == father[b]) printf("Yes\n");
else printf("No\n");
}
return 0;
}
1118 Birds in Forest
最新推荐文章于 2022-10-02 14:45:00 发布