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. java二分法搜索

    二分法就是要将数据每次都分成两份然后再去找到你想要的数据 在二分法查找时要求传入的数据必须已经有序,假设现在为升序,然后每次将所寻找的值与中间值(数组左边界+(右边界-左边界)/2)作比较,大了则去寻 ...

  2. python之科学函数课——Numpy

    一般来讲,数据都是由行列表示的,也就是矩阵,类似于Excel表格一样的东西. 首先我们学习一下Numpy,装好anaconda之后默认是装好的,下面是numpy的一些函数库:Numpy是科学计算库,是 ...

  3. 解题(ConflictPhoneNumber--冲突的电话号码)

    题目描述 移动推出的校内网短号和亲情网短号非常方便,但在某款新手机里却出现了尴尬的bug.例如,当通讯录中包含如下号码时:1.小王:6002.小李:4676543.小张:600010输入600时,手机 ...

  4. win10卸载IE11后从新安装

    win10卸载IE11后想要从新安装,运气不好的话各种坑安装不上,运气好就不说了.这里笔者在知乎看到一种方法,亲测可用,记录如下: 设置--应用--应用和功能--管理可选功能--添加功能--Inter ...

  5. PHP+Mysql 实现数据库增删改查(原生)

    Mysql数据库创建 创建一个新闻列表的数据库: 1. 查询数据库 1.1. 创建文件dbconfig.php,保存常量 <?php define("HOST"," ...

  6. MySql 游标定义时使用临时表

    参考:Re: Temp Table in Select of a Cursor 方法一: delimiter $$ create procedure test_temp() begin drop te ...

  7. server 打开失败

    server:An unexpected exception was thrown. 当server服务器遇到这样遇到不能料想的错误导致打开失败的情况下,我们可以找到一个com.genuitec.ec ...

  8. 爬坑记-tomcat 项目启动两次的的解决

    项目就启动了两次,程序倒是正常运行,关键我里边写了个while 循环,不能让它启动两次啊 百度了一下,有人说是tomcat server.xml或者tomcat新建服务的时候设置出了问题 ....最终 ...

  9. MySQL实现阶段累加的sql写法 ,eq:统计余额

    最近项目碰到一个新的需求,统计每日充值/消费之后的余额.对于这种需求,其实也很简单,只需要在每次充值/消费后,计算下余额,然后保存下来就可以了.但是对于这种需求,一条sql就能搞定,都不需要做冗余字段 ...

  10. Angular实现动态添加删除表单输入框功能

    <div class="form-group form-group-sm" *ngFor="let i of login"> <label c ...