POJ2443_状压的简单使用

1.题目链接。题意就是首先给你n个集合,询问m次,每次输入a,b.判断是否存在一个集合同时含有a,b.这个题就别想着暴力了,我们使用bitset状压一下,自己手动的模拟一下就知道原理了。代码如下:

#include<iostream>
#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;
}

猜你喜欢

转载自blog.csdn.net/weixin_41863129/article/details/85645103