标签:相同 数字 需要 rds 其他 end 一个 输出 void
#include<bits/stdc++.h>
using namespace std;
int father[10010];
bool visit[10010] = {false};
set<int> fa;//用于存放父亲结点
int findF(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 = findF(a);
int faB = findF(b);
if(faA != faB){
father[faB] = faA;
}
}
int main(){
int n, k, cnt = 0, all_bird = 0;
scanf("%d", &n);
for(int i = 0; i < 10010; i++){
father[i] = i;
}
for(int i = 0; i < n; i++){
int num, id;
scanf("%d %d", &num, &id);
visit[id] = true;
for(int j = 1; j < num;j++){
int index;
scanf("%d", &index);
visit[index] = true;
Union(id, index);
}
}
for(int i = 0; i < 10010; i++){
if(visit[i] && fa.find(findF(i)) == fa.end()){
fa.insert(findF(i));
cnt++;
}
if(visit[i]) all_bird++;
}
printf("%d %d\n", cnt, all_bird);
scanf("%d", &k);
for(int i = 0; i < k; i++){
int a, b;
scanf("%d %d", &a, &b);
if(findF(a) == findF(b))printf("Yes\n");
else printf("No\n");
}
return 0;
}
标签:相同 数字 需要 rds 其他 end 一个 输出 void
原文地址:https://www.cnblogs.com/tsruixi/p/12995641.html