poj 2443 bitset 或 状态压缩
来源:互联网 发布:机器人运动仿真软件 编辑:程序博客网 时间:2024/05/22 03:42
题目链接 http://poj.org/problem?id=2443
Set Operation
Time Limit: 3000MS Memory Limit: 65536KTotal 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
33 1 2 33 1 2 51 1041 31 53 51 10
Sample Output
YesYesNoNo
法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;}
阅读全文
0 0
- poj 2443 bitset 或 状态压缩
- poj 3740 Easy Finding//Dancing Links 或 状态压缩Dp
- poj 2443 Set Operation(思维&状态压缩)
- POJ 2923 状态压缩
- poj 2411 (状态压缩)
- poj 3311 状态压缩
- POJ 3254 状态压缩
- (3254)POJ-状态压缩
- (1185)POJ-状态压缩
- (2817)POJ-状态压缩
- (3311)POJ-状态压缩
- POJ-1143(状态压缩)
- poj 1038 状态压缩
- poj 1185 状态压缩
- poj 2686 状态压缩
- POJ-2686-状态压缩
- 状态压缩入门 POJ
- poj-3254 状态压缩
- 树莓派raspbian资源下载
- 图片预加载之无序加载
- 如何在 Git 里撤销(几乎)任何操作
- eclipse使用心得
- JavaScript中关于事件绑定、冒泡、捕获和执行顺序
- poj 2443 bitset 或 状态压缩
- USACO-Section1.1 Broken Necklace [动态规划]
- [安卓基础]学习第七天
- 归并排序
- 最长回文串
- 【七日牧函】来一场爱心大冒险
- python的web开发框架django学习笔记
- 排序--折半插入排序
- BANANAPI 修改启动直接进入系统