codeforces CF986C AND Graph 建圖 dfs
$ \rightarrow $ 戳我進CF原題
C. AND Graph
time limit per test: 4 seconds memory limit per test: 256 megabytes input: standard input output: standard output
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.
Input
In the first line of input there are two integers $ n $ and $ m ( 0 \le n \le 22, 1 \le m \le 2^n ) $ ,
In the second line there are $ m $ integers $ a_1,a_2, \dots ,a_m ( 0 \le a_i \le 2^n ) $ — the elements of the set.
All $ a_i $ are distinct.
Output
Print the number of connected components.
Examples
input1
2 31 2 3
output1
2
input2
5 55 19 10 20 12
output2
2
Note
Graph from first sample:
Graph from second sample:
題目大意
給定 $ m $ 個 $ 0 ~ 2^n-1 $ 之間的整數,每個整數代表一個點
兩個整數 $ x,y $ 之間有無向邊當且僅當 $ x $ & $ y=0 $ ,求無向圖有多少個連通塊
$ n \le 22 $
題解
把 $ 0 ~ 2^n-1 $ 之間的每個點拆成 $ x $ 和 $ x' $ 兩個點
$ 1. $ 從 $ x $ 到 $ (~x)' $ 連有向邊
$ 2. $ 從 $ x' $ 到 $ (x \quad xor \quad (1 \ll k ))' $ 連有向邊 $ (o \le k < n ) $
$ 3. $ 若 $ x $ 屬於給定的 $ m $ 個數,則從 $ x' $ 到 $ x $ 連有向邊
從 $ m $ 個數出發進行遍歷,求連通塊數
代碼
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn (1<<23)+5
int n,m,ans,tot;
bool vis[maxn],mark[maxn];
void dfs(int u){if(vis[u]) return; vis[u]=1;if(u<(1<<n)) dfs(u+(1<<n));else {tot=(1<<(n+1))-1-u;if(!vis[tot]&&mark[tot]) dfs(tot);for(int i=0;i<n;++i) if(!vis[u|(1<<i)]) dfs(u|(1<<i));}
}
int main(){scanf("%d %d",&n,&m);for(int x,i=1;i<=m;++i){ scanf("%d",&x); mark[x]=1; }for(int i=0;i<(1<<n);++i) if(mark[i]&&!vis[i]){ ++ans; dfs(i); }printf("%d",ans);return 0;
}
/*
# 40059473
When 2018-07-07 14:30:49
Who PotremZ
Problem C - AND Graph
Lang GNU C++
Verdict Accepted
Time 764 ms
Memory 91100 KB
*/
转载于:https://www.cnblogs.com/PotremZ/p/9600617.html
codeforces CF986C AND Graph 建圖 dfs相关推荐
- 链式前向星模板 建图+dfs+bfs+dijkstra
边没有用struct封装起来,节点和边的计数起点如果不符合习惯可以稍作修改 建图+DFS+BFS #include <cstdio> #include <cstring> #i ...
- boost::graph模块实现DFS parenthesis的测试程序
boost::graph模块实现DFS parenthesis的测试程序 实现功能 C++实现代码 实现功能 boost::graph模块实现DFS parenthesis的测试程序 C++实现代码 ...
- boost::graph模块实现DFS算法的测试程序
boost::graph模块实现DFS算法的测试程序 实现功能 C++实现代码 实现功能 boost::graph模块实现DFS算法的测试程序 C++实现代码 #include <boost/g ...
- Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...
- CodeForces - 817F Graph and String(dfs判二分图)
题目链接:点击查看 题目大意:给出一张图,现在要求给出一种合适的染色方案,使得: 只能用 ' a ' , ' b ' , ' c ' 进行染色 有边相连的两个点的颜色要么相同,要么相邻,不能是 ' a ...
- 解题报告:luogu P3916 图的遍历( 缩点 + DFS ? × 思维 + 反向建边 + DFS √ )
题目链接:图的遍历 本题是一个有向图,要求每个点能到达的编号最大的点.由于是有向图,如果直接DFS如果有环就可能忽略一些点,所以我们可以直接缩点 缩点之后 在同一个强联通分量中的点可以相互到达,那么我 ...
- vector邻接表建图+DFS+BFS
以边操作为主的图用边集数组存储比较好,相比链式前向星,vector建图更容易懂. #include <iostream> #include <cstdio> #include ...
- CodeForces - 1494E A-Z Graph(构造+思维)
题目链接:https://vjudge.net/problem/CodeForces-1494E 题目大意:给出一个初始时只有 nnn 个点的有向带权图,需要执行 mmm 次操作,每次操作分为下列三种 ...
- Codeforces 1093D Beautiful Graph
题目链接:http://codeforces.com/contest/1093/problem/D 第一次做这种题目,染色+组合数学.. 假设这个图里有奇数环,那么肯定是不行的,因为无论怎么样都是偶数 ...
最新文章
- 李德仁院士:5G时代空间信息如何实现智能服务
- java split 逗号_咦,Java拆分个字符串都这么讲究
- 如何将PHP对象数组转换成普通数组
- Winform让扫描枪听话,防止在有焦点的地方就扫码输入的尴尬
- 汇编语言(三)之判断数值是否大于42H并统计个数
- 【毕业前夕,畅谈科研】一个清华土著的科研感想
- 红利,本质上来自于供求关系的不平衡
- Android给scrollView截图超过屏幕大小形成长图
- absolute绝对定位的参考坐标和参考对象问题详解
- 181117每日一句
- 在linux服务器上安装sublime编辑器
- Batch size对训练效果的影响
- 易居(个人中心资料修改+实名认证)
- 50款PS完美汉化插件一键安装,win+mac
- 制造上云 佛山南海携手阿里云建创新中心
- 新东方托福词汇(List 36 ~ List 40)
- 前端通过【bitly】Api,将长链接转换短连接
- 高数_第5章常微分方程_二阶线性微分方程解的结构
- C#中什么是类,类和对象的关系,类的访问修饰符?
- 安徽省池州市谷歌卫星地图下载
热门文章
- thinkphp三级分销小程序源码_山东谷道微信小程序商城源码带后台 公众号平台三级分销系统...
- json tcl_确定TCL中变量的类型
- MySQL 基础 ———— 变量
- 继上一篇博客--javaweb通过接口来实现多个文件压缩和下载(包括单文件下载,多文件批量下载)
- matlab如何导入多文本数据,将文本文件中的混合数据导入表
- python中集合运算_入门 | 一文带你了解Python集合与基本的集合运算
- linux 终端显示白底,mac终端使用Item2无法显示颜色的解决方法
- mysql schema命名规范_数据库设计 命名及编码规范
- pta7-3 统计不及格人数_应用统计专硕院校专业难度分析【青岛大学、苏大、西安交通大学】...
- css 回到首页按钮,js+css实现回到顶部按钮(back to top)