poj 2443 bitset 或 状态压缩
2017-05-20 17:44
253 查看
题目链接 http://poj.org/problem?id=2443
Set Operation
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
Sample Output
法1:使用bitset 集合
法2:状态压缩
由于输入数据范围不超过int, 可以用32位数组存下。
用c++交, G++超时。
Set Operation
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 3235 | Accepted: 1317 |
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; }
相关文章推荐
- poj 2443 状态压缩 叉姐的魔法训练
- poj 1077 Eight 八数码问题( 康拓展开+BFS状态压缩)
- HDU 1400 (POJ 2411 ZOJ 1100)Mondriaan's Dream(DP + 状态压缩)
- poj2288(状态压缩dp)
- poj 2411 Mondriaan's Dream(状态压缩dp)
- POJ 2923 Relocation(状态压缩+ 01背包)
- poj 2686 状态压缩
- Poj1038 Bugs Integrated, Inc.【状态压缩DP】
- poj 2440 DNA(状态压缩递推+矩阵)
- poj 3311 (状态压缩dp)
- POJ 3311 Hie with the Pie 状态压缩DP+floyd
- POJ 3311 Hie with the Pie【状态压缩DP】
- Poj-3570 Fund Management(状态压缩动态规划)
- poj 1185 状态压缩
- poj 2411 Mondriaan's Dream 状态压缩DP
- POJ 1185 炮兵阵地 经典状态压缩DP
- POJ 1038 Bugs Integrated, Inc.(状态压缩)
- POJ 1739 Tony's Tour, 连通性状态压缩DP
- POJ 1038 Bug Integrated Inc(状态压缩DP)
- POJ 1681 Painter's Problem【状态压缩,枚举】