Codeforces Round #485 (Div. 2) F. AND Graph

题目连接:

http://codeforces.com/contest/987/problem/F

Description

You are given a set of size $m$ with integer elements between $0$ and $2^{n}-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.

Sample Input

2 3
1 2 3

Sample Output

2

题意

有n个点,每个点有一个值x,如果\(x&y=0\),则两点之间连一条边,问最后有多少联通块

There are n point, each point has its value. If \(x&y=0\), connect the two point with a edge. Print the number of connected components.

题解:

考虑最暴力的,对于一个数,枚举出所有求位运算和后使他为0的数字,然后判断该数字存在与否。这样会导致很多无用枚举。反向思考,该点能排除哪些点呢?对于\(2^{22}\)所有数字搜一次,最多只搜一次,时间在上限之内。

Consider a number X. If we figure out the all number which make \(X&number=0\). It's too complex. If we add this number, it can exclude the number . So the number will be search for at most once.

代码

#include <bits/stdc++.h>

using namespace std;

int n, m, ans;
bool vis[1 << 22];
bool ext[1 << 22];
int x;
vector<int> v; inline void dfs(int k) {
if (vis[k]) return;
vis[k] = 1;
if (ext[k]) dfs(k ^ ((1 << n) - 1));
for (int i = 0; i < n; i++)
if (k & (1 << i)) dfs(k ^ (1 << i));
} int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr); cin >> n >> m;
for (int i=0;i<m;i++) {
cin>>x;
v.push_back(x);
ext[x]=1;
}
for (auto i:v) {
if (!vis[i]) {
ans++;
vis[i] = 1;
dfs(i ^ ((1 << n) - 1));
}
}
cout << ans << endl;
}

Codeforces Round #485 (Div. 2) F. AND Graph的更多相关文章

  1. Codeforces Round #485 (Div. 2)

    Codeforces Round #485 (Div. 2) https://codeforces.com/contest/987 A #include<bits/stdc++.h> us ...

  2. Codeforces Round #485 (Div. 2) D. Fair

    Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...

  3. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

  4. Codeforces Round #485 (Div. 2) C. Three displays

    Codeforces Round #485 (Div. 2) C. Three displays 题目连接: http://codeforces.com/contest/987/problem/C D ...

  5. Codeforces Round #485 (Div. 2) A. Infinity Gauntlet

    Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...

  6. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  7. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  8. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  9. Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

随机推荐

  1. 【第二组】Hunter——beta版发布文档

    软件测试报告 一.bug情况汇总 尚需解决以及难以解决的: 登录时会有卡顿,需要加入加载进度条(会添加的) 商城和背包功能尚未实现(需要修复) 美工水平太差,让人没有使用的欲望(大概接下来就专门做这个 ...

  2. jenkins 多版本 jdk

    最近又开始使用jenkins了,遇到一个问题,开发本地使用的jdk是1.7,而我们jenkins由于需要对docker的插件的支持必须使用1.8. 这样就导致了有的开发的代码在jenkins去代码库c ...

  3. 开源虚拟化KVM(三)管理虚拟网络

    六,管理虚拟网络 [x] Linux网桥基本概念 [x] qemu-kvm支持的网络 [x] 向虚拟机添加虚拟网络连接 [x] 基于NAT的虚拟网络 [x] 基于网桥的虚拟网络 [x] 用户自定义的隔 ...

  4. SourceTree提交不了,报git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags origin master:master

    刚下载好的Soucetree,拉好项目代码却提交不了,害的我百度了好一小会,下面我把我自己最终的解决方案介绍给大家,希望对你们有用. 首先打开 下载好的git 输入命令  ssh-keygen -t  ...

  5. 【练习】Python第一,二次

    练习一 1,执行Python脚本的两种方式 a,Python解释器 b,Python  1.py 2,简述位和字节的关系 一个字节等于8位 3,简述ascii,unicode,utf-8,gbk的关系 ...

  6. pandas,对dataFrame中某一个列的数据进行处理

    背景:dataFrame的数据,想对某一个列做逻辑处理,生成新的列,或覆盖原有列的值   下面例子中的df均为pandas.DataFrame()的数据   1.增加新列,或更改某列的值 df[&qu ...

  7. Debian 9 Stretch国内常用镜像源

     随着Debian 9的普及,但由于伟大的墙的存在,那就有必要整理一下国内的镜像站点. 1.使用说明 一般情况下,修改/etc/apt/sources.list文件,将Debian的默认源地址改成新的 ...

  8. Vue数据交互

    注意:本地json文件必须放在static目录下面,读取或交互数据前,请先安装vue-resource.点击前往  -->(vue-resource中文文档) 一.Vue读取本地JSON数据 c ...

  9. 关于Image创建的内存管理

    image创建方法 [UIImage imageNamed:imageName] 上述方法创建的image,会常驻在内存中,不会随着imageView的dealloc而释放内存. NSString * ...

  10. inline, block, and inline-block

    总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).block元素通 ...