判断两个元素是否在同一集合。
solution: 每个元素开hash记录该元素在那些集合内,压位+位运算优化即可~
#include<cstdio>
#include<cstdlib>
#include<algorithm>
const int MAXN = 1005, MAXM = 1e4+5;
int n, s, Q;
int ins[MAXM][40] = {0};
int main()
{
#ifndef ONLINE_JUDGE
freopen("poj2443.in","r",stdin);
freopen("poj2443.out","w",stdout);
#endif
scanf("%d",&n); s = n/30 + (int)(n%30!=0);
for(int i = 1, ci; i <= n; i++)
{
scanf("%d",&ci);
int s1 = i/30, s2 = i%30;
for(int j = 1, p; j <= ci; j++)
{scanf("%d",&p); ins[p][s1] |= 1 << s2;}
}
scanf("%d",&Q);
while(Q--)
{
int x,y;
bool flag = false;
scanf("%d%d",&x,&y);
for(int i = 0; i <= s; i++)
if(ins[x][i]&ins[y][i])
{flag = true; break;}
puts(flag?"Yes":"No");
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}