https://www.patest.cn/contests/pat-a-practise/1118
吐槽:这是道并查集的题,没有什么好说的,但是可以钻题目的空子还是要钻的,因为鸟的索引是连续的,所以鸟的总数就是其中最大的数,在输入的时候保存下来就行了,最后就是树的组数,主要把并查集构建起来,组数只要记录父节点等于自己本身的点。
#include<cstdio>
#include<cstring>
#define N 10010
int father[N];
int find_father(int z){
if(father[z] == z) return z;
else{
int tp = find_father(father[z]);
father[z] = tp;
return tp;
}
}
void Union(int a,int b){
int faA = find_father(a);
int faB = find_father(b);
if(faA != faB)
father[faA] = faB;
}
int main(){
int n,k,q,x,y,tmp,max=0,id;
memset(father,0,sizeof(father));
scanf("%d",&n);
for(int i = 0;i < N; i++){
father[i] = i;
}
while(n--){
scanf("%d%d",&k,&id);
if(max < id) max = id;
for(int i=0;i<k-1;i++){
scanf("%d",&tmp);
Union(id,tmp);
if(max < tmp) max=tmp;
}
}
int cnt = 0;
for(int i=1;i<=max;i++){
if(father[i] == i)
cnt++;
}
printf("%d %d\n",cnt,max);
scanf("%d",&q);
for(int i=0;i<q;i++){
scanf("%d%d",&x,&y);
if(find_father(x) != find_father(y))
printf("No\n");
else
printf("Yes\n");
}
return 0;
}