poj2443Set Operation (bitset)
2016-03-04 09:32
387 查看
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
题意:有n个集合,每个集合里有c[i]个数,可能重复,共有m个操作,每个操作询问两个数,问这两个数是否在n个集合中的某一个同时出现。
思路:一开始标记每一个集合中出现的数,然后O(n*m)的复杂度T了,换了bitset的思路,即用bitset<1005>bt[10005]记录第i个元素在第j个集合出现的情况,然后对于任意两个数a,b,只要用(bt[a]&bt[b]).any()判断一下是否出现过就行。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
#include<bitset>
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1005
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef long double ldb;
bitset<1005>bt[10005];
int main()
{
int n,m,i,j,c,d;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=10000;i++)bt[i].reset();
for(i=1;i<=n;i++){
scanf("%d",&c);
for(j=1;j<=c;j++){
scanf("%d",&d);
bt[d][i]=1;
}
}
scanf("%d",&m);
for(i=1;i<=m;i++){
scanf("%d%d",&c,&d);
if((bt[c]&bt[d]).any() )printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
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 NoNo
题意:有n个集合,每个集合里有c[i]个数,可能重复,共有m个操作,每个操作询问两个数,问这两个数是否在n个集合中的某一个同时出现。
思路:一开始标记每一个集合中出现的数,然后O(n*m)的复杂度T了,换了bitset的思路,即用bitset<1005>bt[10005]记录第i个元素在第j个集合出现的情况,然后对于任意两个数a,b,只要用(bt[a]&bt[b]).any()判断一下是否出现过就行。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
#include<bitset>
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1005
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef long double ldb;
bitset<1005>bt[10005];
int main()
{
int n,m,i,j,c,d;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=10000;i++)bt[i].reset();
for(i=1;i<=n;i++){
scanf("%d",&c);
for(j=1;j<=c;j++){
scanf("%d",&d);
bt[d][i]=1;
}
}
scanf("%d",&m);
for(i=1;i<=m;i++){
scanf("%d%d",&c,&d);
if((bt[c]&bt[d]).any() )printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
相关文章推荐
- Linux音视频(SDL库)
- 关于tomcat启动报错:java.io.EOFException
- linux下jdk安装
- eclipse下的tomcat内存设置大小
- Linux下查看文件内容的命令
- Linux 添加开机启动项的两种方法
- linux的svn服务器搭建--Subversion Edge
- linux常用命令
- 嵌入式Linux开发学习笔记:编译与调试
- shell循环
- linux下配置lamp
- 使用 adb shell 抓取 Android 性能数据
- 笔记:Linux常用命令(七)磁盘管理
- Nginx+CI框架出现404错误怎么解决
- 专访阿里王峰:Hadoop生态下一代计算引擎-streaming和batch的统一
- SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论
- Linux文件系统
- Linux用户和用户管理
- Linux磁盘管理
- [国嵌攻略][099][Linux内核配置与编译]