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. SpringMVC中的一些注解

    @Controller:表明该类是一个Controller: @RequestMapping(参数) :为类或者方法定义一个url @RequestParam(value = "id&quo ...

  2. 初识docker

    什么是docker Docker 最初是 dotCloud 公司创始人 Solomon Hykes 在法国期间发起的一个公司内部项目,于 2013 年 3 月以 Apache 2.0 授权协议开源,主 ...

  3. python day11 函数(第三篇)

    2019.4.11 S21 day11笔记总结 1. 函数小高级 ( 5* ) 1 函数名可以当作变量来使用 def func(): print(123) v1 = func # func代表函数的地 ...

  4. SocketIO Server

    package com.fd.socketio; import org.json.JSONObject; import com.corundumstudio.socketio.AckRequest; ...

  5. 用eclipse创建动态web项目手动生成web.xml方法

    建一个web项目,后来在用到web.xml文件时,才发现项目创建时没有自动创建web.xml文件. 在创建的项目上单击右键,然后单击java EE Tools下的用红线圈住的地方,然后查看你的WEB- ...

  6. 100-days: twenty-six

    Title: The Guardian(英国卫报) view on the Notre Dame fire: we share France's terrible loss Notre Dame 巴黎 ...

  7. wpf 给listview的数据源转换为集合

    目的是点击某个按钮把一条数据从 itemssource中移除 private void delete_Click_1(object sender, RoutedEventArgs e) { DtsIn ...

  8. UILabel设置富文本后不显示省略号

    先描述一下问题,项目中用到了UILabel去显示一段富文本文字,超过label显示区域部分,省略号处理. 但是当设置好 attributedText 给label之后,显示出的效果是文字被切割了,并没 ...

  9. MyEclipse 编译错误 web项目中的 js,jsp报错 更改

    搜索   validation   语法检测 必须 选定一个  不然不编译

  10. iOS.redefinition-of-struct-x

    Error: Redefinition of struct x Reference