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两点之间仅经过黑色变的最短距离. 树链剖分 ...
随机推荐
- 解决perm size out of memeory的问题
在idea中配置如下即可 -Xms1024m -Xmx1024m -XX:MaxNewSize=512m -XX:MaxPermSize=512m 如下图所示:
- (转)Django常用命令
转自GoodSpeed,http://www.cnblogs.com/cacique/archive/2012/09/30/2709145.html . . . . .
- ActiveMQ(2) ActiveMQ创建HelloWorld
启动ActiveMQ: 请参见:ActiveMQ(1) 初识ActiveMQ 创建Maven工程: pom文件: <project xmlns="http://maven.apache ...
- 《vue.js实战》练习---标签页组件
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- idea控制台中文乱码问题解决办法
解决办法: 打开Intellij的安装的bin目录(D:\Program Files\JetBrains\IntelliJ IDEA 14.0\bin ),找到上图的两个文件(根据你的系统是32位或6 ...
- 【BZOJ2663】灵魂宝石 [二分]
灵魂宝石 Time Limit: 5 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description “作为你们本体的灵魂,为了能够更好的 ...
- 使用SpringMVC解决Ajax跨域问题
package com.mengyao.system.filter; import java.io.IOException; import javax.servlet.FilterChain; imp ...
- python3 内置函数(转)
http://www.runoob.com/python/python-built-in-functions.html divmod(7,2) # 返回(3,1)商和余的元组 frozenset() ...
- Swift 闭包(六)
http://blog.csdn.net/huangchentao/article/details/32714185 闭包 Closures 1.闭包表达式 闭包表达式是一种利用简单语法构建内联包的方 ...
- POJ3264(线段树求区间最大最小值)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41162 Accepted: 19327 ...