Codeforces 986C AND Graph dfs
原文链接https://www.cnblogs.com/zhouzhendong/p/9161514.html
题目传送门 - Codeforces 986C
题意
给定 $n,m (0\leq n\leq 22,1\leq m\leq 2^n)$ 。
接下来给定 $m$ 个数,记第 $i$ 个数为 $a_i$ ,对于所有 $a_i$ ,满足 $0\leq a_i\leq 2^n$ 。
第 $i$ 个数与第 $j$ 个数有无向边,当且仅当 $a_i\ AND\ a_j=0$ 。其中 $"AND"$ 是按位与。
问在以这 $m$ 个数为节点的无向图中有多少个各自独立的连通块。
题解
考虑有有连边的条件。
我们记 $"AND"$ 为按位与运算, $"OR"$ 为按位或运算, $"XOR"$ 为按位异或运算。
我们记 $s=2^n-1$ 。
如果 $a$ 与 $b$ 有连边,那么满足 $b \in {x| x\ OR\ (s\ XOR\ a) = (s\ XOR\ a) }$。
于是我们考虑记忆化dfs。
我们用 $v[y]$ 表示集合 ${x|x\ OR\ y=y}$ 是否被访问过。
在 dfs 的过程中,dfs 一个 $y$ ,我们就要访问其所有子集。
如果当前的 $y$ 在 $a$ 数组中出现过,那么我们确定了上一个数与当前数的连通关系,而且我们要继续 dfs,在 $ s\ XOR\ y $ 代表的集合中 dfs 查找是否有新的数字连通。
由于连通性具有传递性和对称性,所以每次dfs可以排除一块连通块。
然后就简单统计一下就可以了。
代码
#include <bits/stdc++.h>
using namespace std;
const int N=1<<22;
int n,m,s,a[N],f[N],v[N];
void dfs(int x){
if (v[x])
return;
v[x]=1;
if (f[x])
dfs(s^x);
for (int i=0;i<n;i++)
if (x&(1<<i))
dfs(x^(1<<i));
}
int main(){
scanf("%d%d",&n,&m);
s=(1<<n)-1;
memset(f,0,sizeof f);
memset(v,0,sizeof v);
for (int i=1;i<=m;i++)
scanf("%d",&a[i]),f[a[i]]=1;
int ans=0;
for (int i=1;i<=m;i++)
if (!v[a[i]]){
v[a[i]]=1;
dfs(s^a[i]);
ans++;
}
printf("%d",ans);
return 0;
}
Codeforces 986C AND Graph dfs的更多相关文章
- Codeforces 986C - AND Graph(dfs)
Codeforces 题面传送门 & 洛谷题面传送门 考虑 DFS 一遍遍历每个连通块. 当我们遍历到一个点 \(x\) 时,我们就建立一个虚点 \((2^n-1-x)'\) 表示我们要访问 ...
- CodeForces - 986C AND Graph
不难想到,x有边连出的一定是 (2^n-1) ^ x 的一个子集,直接连子集复杂度是爆炸的...但是我们可以一个1一个1的消去,最后变成补集的一个子集. 但是必须当且仅当 至少有一个 a 等于 x 的 ...
- [Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...
- [Codeforces 163D]Large Refrigerator (DFS+剪枝)
[Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)
数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...
- CF 986C AND Graph(建模+DFS)
#include<stdio.h> ],v[]; ],n,al; void dfs(int x){ if(v[x])return; v[x]=; if(ex[x])dfs(al^x); ; ...
- Educational Codeforces Round 25 Five-In-a-Row(DFS)
题目网址:http://codeforces.com/contest/825/problem/B 题目: Alice and Bob play 5-in-a-row game. They have ...
- Codeforces 838B - Diverging Directions - [DFS序+线段树]
题目链接:http://codeforces.com/problemset/problem/838/B You are given a directed weighted graph with n n ...
随机推荐
- 前端 ----jQuery的介绍
01-jQuery的介绍 1.为什么要使用jQuery 在用js写代码时,会遇到一些问题: window.onload 事件有事件覆盖的问题,因此只能写一个事件. 代码容错性差. 浏览器兼容性问题 ...
- oracle 定时 job
最近在工作中遇到了要在oracle里面创建一个定时job,从远程数据库里面定时把某张表里面的数据更新到本地服务器上某个表里,具体操作: 1.在自己数据库里面先创建一张表test create tabl ...
- zipkin 整合elastic
前提: <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin</ar ...
- Struts2配置拦截器
<package name="loginaction" namespace="/" extends="struts-default"& ...
- liux三剑客grep 正则匹配
001正则匹配(大部分需要转义) ‘^‘: 锚定行首 '$' : 锚定行尾 [0-9] 一个数字 [^0-9] 除去数字所有,^出现在[]这里表示取反 [a-z] [A-Z] [a-Z] \s 匹配空 ...
- swift 实践- 04 -- UIButton
import UIKit class ViewController: UIViewController { // 按钮的创建 // UIButtonType.system: 前面不带图标, 默认文字为 ...
- Mac配置Jdk 安装及系统环境配置
注:本文来于< Mac配置Java开发环境 > 1. 下载JDK 从下面链接选择合适版本的安装包进行下载...笔者下载的是jdk-9.0.1 链接:http://www.oracl ...
- [原著]java或者Js 代码逻辑来处理 突破 oracle sql “IN”长度的极限的问题
注:本文出自:博主自己研究验证可行 [原著]java或者Js 代码逻辑来处理 突破 oracle sql "IN"长度的极限的问题 在很多的时候 使用 select ...
- ssd.pytorch
https://towardsdatascience.com/learning-note-single-shot-multibox-detector-with-pytorch-part-1-38185 ...
- py4j汉语转拼音多音字处理
先看下效果 一 .布局 <!-- 上面的搜索框 --> <com.example.editablealphalist.widgget.ClearEditText android:id ...