【codeforces986C】AND Graph

time limit :per test 4 seconds
memory limit :per test 256 megabytes
standard input
standard output

Description
You are given a set of size m with integer elements between 0 and 2n−1 inclusive. Let’s build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x&y=0. Here & is the bitwise AND operation. Count the number of connected components in that graph.

Input
In the first line of input there are two integers n and m (0≤n≤22, 1≤m≤2n).

In the second line there are m integers a1,a2,…,am (0≤ai<2n) — the elements of the set. All ai are distinct.

Output
Print the number of connected components.

Examples
input1

2 3
1 2 3

output1

2

input2

5 5
5 19 10 20 12

output2

2

题目大意

就是给出小于 222 2 22 的一些数,两个数有边的条件为and值为0
求联通块个数

如果直接连边肯定会T飞掉, 这时思考连辅助边,a and b的另一种表现形式是 b b ∈ ~a,其中~a表示a的反码,也就是 2n1a 2 n − 1 − a
那么可以有三种走法
(x,1)(x,2) ( x , 1 ) → ( x , 2 )
(x,2)(y,2) ( x , 2 ) → ( y , 2 ) y&x=x且x^y= 2k 2 k
(x,2)(y) ( x , 2 ) → ( y ) y=~x

如果存在路径 (a,1)(x1,2)(x2,2)(xk,2)(b,1)) ( a , 1 ) → ( x 1 , 2 ) → ( x 2 , 2 ) → ⋯ → ( x k , 2 ) → ( b , 1 ) ) 那么a,b右边
这样的话就可以跑dfs了若a[i]没走过就跑一遍

code

#include<cstring>
#include<cstdio>
#include<bitset>
#define N 8800000

using namespace std;

int n,m,a,ans,k,x[N];
bitset<N> vis,app;

void dg(int w){
    int x=w>>1;
    if(w&1){
        vis[w]=1;
        if(!vis[a+a-2-x-x])dg(a+a-2-x-x);
        for(int k=1;k<a;k<<=1)if((x&k)==0)if(!vis[2*(x+k)+1])dg(2*(x+k)+1);
    }else{
        if(!app[x])return;
        vis[w]=1;
        if(!vis[x+x+1])dg(x+x+1);
    }
}

int main(){
    scanf("%d %d",&n,&m);a=1<<n;
    for(int i=1;i<=m;i++){
        scanf("%d",&x[i]);app[x[i]]=1;
    }
    for(int i=1;i<=m;i++)if(!vis[x[i]+x[i]])++ans,dg(x[i]+x[i]);
    printf("%d",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值