题意:给出一些集合,然后判断两个数是否在同一个集合中。
思路:对于每一个元素,构建一个bitset,然后如果它在i这个元素中的话,这个bitset的这个值变成1。查询的时候,直接看两个元素对应的bitset与一下是否有1出现,有的话就说明有在同一个集合。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
const int maxn = 10000 + 10;
bitset<maxn>g[maxn];
int main()
{
int n,m;
while( ~ scanf("%d",&n))
{
for(int i = 0; i < maxn; i ++)g[i].reset();
for(int i = 1; i <= n; i ++)
{
int x,y;scanf("%d",&x);
for(int j = 1; j <= x; j ++)scanf("%d",&y),g[y].set(i);
}
scanf("%d",&m);
while(m --)
{
int x,y;
scanf("%d%d",&x,&y);
bitset<maxn> t = g[x] & g[y];
if(t.any())
puts("Yes");
else puts("No");
}
}
return 0;
}