CodeForces - 986C AND Graph
不难想到,x有边连出的一定是 (2^n-1) ^ x 的一个子集,直接连子集复杂度是爆炸的。。。但是我们可以一个1一个1的消去,最后变成补集的一个子集。
但是必须当且仅当 至少有一个 a 等于 x 的时候, 可以直接dfs(all ^ x) ,否则直接消1连边。。。
Discription
You are given a set of size mm with integer elements between 00 and 2n−12n−1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers xx and yy with an edge if and only if x&y=0x&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 nn and mm (0≤n≤220≤n≤22, 1≤m≤2n1≤m≤2n).
In the second line there are mm integers a1,a2,…,ama1,a2,…,am (0≤ai<2n0≤ai<2n) — the elements of the set. All aiai are distinct.
Output
Print the number of connected components.
Examples
2 3
1 2 3
2
5 5
5 19 10 20 12
2
Note
Graph from first sample:
Graph from second sample:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=5000005;
int ci[233],T,n,a[maxn],ans,all;
bool v[maxn],isp[maxn]; inline int read(){
int x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x;
} void dfs(int x){
if(v[x]) return;
v[x]=1; if(isp[x]) dfs(all^x); for(int i=0;i<=T;i++) if(ci[i]&x) dfs(x^ci[i]);
} inline void solve(){
for(int i=1;i<=n;i++) if(!v[a[i]]){
ans++,v[a[i]]=1,dfs(all^a[i]);
}
} int main(){
ci[0]=1;
for(int i=1;i<=22;i++) ci[i]=ci[i-1]<<1; T=read(),n=read(),all=ci[T]-1;
for(int i=1;i<=n;i++) a[i]=read(),isp[a[i]]=1; solve(); printf("%d\n",ans);
return 0;
}
CodeForces - 986C AND Graph的更多相关文章
- Codeforces 986C AND Graph dfs
原文链接https://www.cnblogs.com/zhouzhendong/p/9161514.html 题目传送门 - Codeforces 986C 题意 给定 $n,m (0\leq n\ ...
- Codeforces 986C - AND Graph(dfs)
Codeforces 题面传送门 & 洛谷题面传送门 考虑 DFS 一遍遍历每个连通块. 当我们遍历到一个点 \(x\) 时,我们就建立一个虚点 \((2^n-1-x)'\) 表示我们要访问 ...
- Codeforces 1093D Beautiful Graph(二分图染色+计数)
题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...
- Codeforces 466E Information Graph
Information Graph 把询问离线之后就能随便搞了, 去check一下是不是祖先, 可以用倍增也能用dfs序. #include<bits/stdc++.h> #define ...
- [Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...
- Codeforces 1288F - Red-Blue Graph(上下界网络流)
Codeforces 题面传送门 & 洛谷题面传送门 好久没有写过上下界网络流了,先来一题再说( 首先先假设所有边都是蓝边,那么这样首先就有 \(b\times m\) 的花费,但是这样不一定 ...
- CodeForces 466E Information Graph --树形转线性+并查集
题意:有三种操作: 1.新增一条边从y连向x,此前x没有父节点 2.x接到一份文件,(文件标号逐次递增),然后将这份文件一路上溯,让所有上溯的节点都接到这份文件 3.查询某个节点x是否接到过文件F 解 ...
- 【CodeForces 624C】Graph and String
题 题意 n个表示abc三个字符的点,所有a和b是相连的,所有b和c是相连的,所有相同的是相连的,现在给你n个点和他们之间的m条边,判断是否存在这样的字符串,存在则给出一个符合条件的. 分析 我的做法 ...
- codeforces 165D.Beard Graph 解题报告
题意: 给一棵树,树的每条边有一种颜色,黑色或白色,一开始所有边均为黑色,有两个操作: 操作1:将第i条边变成白色或将第i条边变成黑色. 操作2 :询问u,v两点之间仅经过黑色变的最短距离. 树链剖分 ...
随机推荐
- activity栈清空
http://blog.csdn.net/swjtuxu/article/details/26163737
- gitlab7.2安装
系统:centos6.4 1.安装依赖包 导入epel: useradd git wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-rel ...
- Python代码规范
一:背景 用于规范化ocp python开发,对于使用python开发的程序使用统一的风格,便于代码的维护 二:python风格规范 分号:不要在行尾加分号,也不要用分号将两条命令放在同一行 括号:宁 ...
- Windows Server 2008 R2 Upgrade Paths
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/dd ...
- 播放video
<html> <head> <title> four in one vedio</title> <style type="text/cs ...
- c++(类) this指针
this指针的相关概念: this只能在成员函数中使用.全局函数,静态函数都不能使用this.实际上,成员函数默认第一个参数为T* const register this. 为什么this指针不能再静 ...
- idea控制台中文乱码问题解决办法
解决办法: 打开Intellij的安装的bin目录(D:\Program Files\JetBrains\IntelliJ IDEA 14.0\bin ),找到上图的两个文件(根据你的系统是32位或6 ...
- bzoj2453/2120 数颜色
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2120 http://www.lydsy.com/JudgeOnline/problem.ph ...
- The service base of EF I am using
using CapMon.Data; using System; using System.Collections.Generic; using System.Linq; using System.T ...
- Linux MMC介绍
1. 介绍 Linux中,将包括MMC.SD.SDIO统称为MMC子系统 MMC子系统从功能上可分为三个层次 - card层: Card驱动, 或称client驱动 - core层: MMC的核心层, ...