L2-024. 部落(并查集+STL)
只要记住第一个数字就好啦!!!!!
然后用set去重!!!
#include<bits/stdc++.h>
using namespace std;
int s[10007], a[10007][10007];
bool sg[10007];
set<int> se;
int fin(int i){
if(s[i] == -1) return i;
else return s[i] = fin(s[i]);
}
int main()
{
memset(s, 255, sizeof(s));
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++){
int k;
scanf("%d",&k);
bool f = true;
cin >> a[i][0];
se.insert(a[i][0]);
for(int j = 1; j < k; j++){
cin >> a[i][j];
se.insert(a[i][j]);
int fx = fin(a[i][0]);
int fy = fin(a[i][j]);
if(fx != fy) s[fy] = fx;
}
}
int ax = se.size();
se.clear();
for(int i = 0; i < n; i++) {
se.insert(fin(a[i][0]));
}
int bx = se.size();
cout << ax << " " << bx << endl;
cin >> n;
while(n--){
int x,y;
scanf("%d%d",&x,&y);
if(fin(x) == fin(y)) puts("Y");
else puts("N");
}
}