您的位置:首页 > 其它

poj 2443 bitset 或 状态压缩

2017-05-20 17:44 253 查看
题目链接 http://poj.org/problem?id=2443

Set Operation

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3235 Accepted: 1317
Description

You are given N sets, the i-th set (represent by S(i)) have C(i) element (Here "set" isn't entirely the same as the "set" defined in mathematics, and a set may contain two same element). Every element in a set is represented by a positive number from 1 to 10000.
Now there are some queries need to answer. A query is to determine whether two given elements i and j belong to at least one set at the same time. In another word, you should determine if there exist a number k (1 <= k <= N) such that element i belongs to
S(k) and element j also belong to S(k).
Input

First line of input contains an integer N (1 <= N <= 1000), which represents the amount of sets. Then follow N lines. Each starts with a number C(i) (1 <= C(i) <= 10000), and then C(i) numbers, which are separated with a space, follow to give the element in
the set (these C(i) numbers needn't be different from each other). The N + 2 line contains a number Q (1 <= Q <= 200000), representing the number of queries. Then follow Q lines. Each contains a pair of number i and j (1 <= i, j <= 10000, and i may equal to
j), which describe the elements need to be answer.
Output

For each query, in a single line, if there exist such a number k, print "Yes"; otherwise print "No".
Sample Input
3
3 1 2 3
3 1 2 5
1 10
4
1 3
1 5
3 5
1 10

Sample Output
Yes
Yes
No
No


法1:使用bitset 集合

#include <cstdio>
#include <iostream>
#include <bitset>
using namespace std;
bitset<1005> bit[10005];

bitset<1005> tmp;
int main()
{
int n, m, q, num;
while(~scanf("%d", &n))
{
for(int i=0; i<1001; ++i)
bit[i].reset();//reset全部置0, set全部置1
for(int i=0; i<n; ++i)
{
scanf("%d", &m);
while(m--)
{
scanf("%d", &num);
bit[num][i]=1;
}
}
scanf("%d", &q);
while(q--)
{
int u, v;
scanf("%d %d", &u, &v);
tmp = bit[u]&bit[v];
if(tmp.count()!=0)
puts("Yes");
else
puts("No");
}
}
return 0;
}

法2:状态压缩  

由于输入数据范围不超过int, 可以用32位数组存下。

用c++交, G++超时。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <vector>
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;
typedef pair<int, int> Pii;
typedef long long LL;
const int MAXN = 1005;
const int inf = 0x3f3f3f3f;
const int Mod = 100000000;
struct node{
int num[400];
void add(int n)
{
int x=n/31, y=n%31;
num[x]|=1<<y;
}
bool judge(int n)
{
int x=n/31, y=n%31;
return num[x]&(1<<y);
}
void init()
{
memset(num, 0, sizeof(num));
}
}a[MAXN];
inline int read()
{
int f=1, x=0;
char ch = getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
int main()
{
int n, m, q, k, flag;
while(~scanf("%d", &n))
{
for(int i=1;i<=n;++i)
a[i].init();
for(int i=1;i<=n;++i)
{
scanf("%d", &m);
while(m--)
{
scanf("%d", &k);
a[i].add(k);
}
}
scanf("%d", &q);
int u, v;
while(q--)
{
scanf("%d %d", &u, &v);
flag=0;
for(int i=1;i<=n;++i)
{
if(a[i].judge(u)&&a[i].judge(v))
{
flag=1;
break;
}
}
if(!flag)
printf("No\n");
else
printf("Yes\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: