[POJ 2443] Set Operation (bitset)

题目链接:http://poj.org/problem?id=2443

题目大意:给你N个集合,每个集合里有若干个数。M个查询,每个查询有a,b两个数。问是否存在一个集合同时包含a,b这两个数。若存在则输出Yes,否则为No。

康神竟然一下子就想出来了。。

思路:统计每个数在哪个集合出现过,用bitset记录下来。然后对于a,b,则把两个bitset取与。

如果得到空集就是No,否则就是Yes。

 1 #include <cstdio>
 2 #include <bitset>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <iostream>
 6
 7 using namespace std;
 8
 9 const int MAX_N = 222222;
10 int N;
11 bitset<1111> bs[MAX_N];
12
13 int main(){
14     while(scanf("%d",&N)!=EOF){
15         for(int i=0;i<MAX_N;i++) bs[i].reset();
16         for(int i=0;i<N;i++){
17             int a,an;
18             scanf("%d",&an);
19             while( an-- ){
20                 scanf("%d",&a);
21                 bs[a].set(i);
22             }
23         }
24         int M;
25         scanf("%d",&M);
26         while(M--){
27             int a,b;
28             scanf("%d%d",&a,&b);
29             bitset<1111> t = bs[a]&bs[b];
30             if( t.count() ){
31                 puts("Yes");
32             } else {
33                 puts("No");
34             }
35         }
36     }
37
38     return 0;
39 }
时间: 2024-10-06 23:36:08

[POJ 2443] Set Operation (bitset)的相关文章

POJ 2443 - Set Operation

注意变换思维,然后就是水题.(如果卡时还可以进一步二分优化.) #include <stdio.h> long existed[10000][32]; int main(void) { int N; // freopen("poj2443.txt", "r", stdin); scanf("%d", &N); int i, j; for(i=0; i<N; ++i) { int C, k; scanf("%d&

poj 2443

Set Operation Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2961   Accepted: 1192 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" defi

【搭楼】做题记录

以后做了题还是在这里写一下,觉得好的再去发题解(感觉无脑发题解意义不大) 也不一定是做了的题,看了没打但觉得不错的也可以发上来 (5.23-5.24 第三次月考被X得相当爽) 5.23 星期六 [贪心]Bzoj4027 HEOI2014 兔子与樱花 要是父亲合并儿子又合并就混乱了.然后发现,反正贡献都是一?能合并就在儿子处合并?贪心. [分块]Bzoj3343 教主的魔法 做之前知道了tag,于是很快就想到了算法.还没打过分块呢,于是先去膜拜了一下别人的代码.自己打出来后各种WA,太晚了没调出来

bitset骚操作

bitset的用途挺多的,是一个比较骚的常数优化 一.很多位数的二进制数 poj 2443 http://poj.org/problem?id=2443 直接开个1万位的二进制数,求交就行了. 有关集合求并交的时候可以考虑biset优化 #include<bitset> #include<cstdio> #define REP(i, a, b) for(register int i = (a); i < (b); i++) #define _for(i, a, b) for(

[POJ2443]Set Operation(bitset)

传送门 题意:给出n个集合(n<=1000),每个集合中最多有10000个数,每个数的范围为1~10000,给出q次询问(q<=200000),每次给出两个数u,v判断是否有一个集合中同时含有u,v两个数 枚举每一个集合,看看是否同时又u和v,显然超时 用bitset维护每一个数所在集合,求解的时候直接即可u & v #include <cstdio> #include <bitset> using namespace std; int n, m; bitset

POJ2443 Set Operation (基础bitset应用,求交集)

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 posit

POJ 1077 Eight(康托展开+BFS)

Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special Judge Description The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15

POJ 3468 伸展树建树

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59628   Accepted: 18180 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

[2016-01-27][POJ][2230][Watchcow]

H - 欧拉图 中级者向 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2230 Description Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the farm and make s