Codeforces 987 F - AND Graph
思路:
首先,x & (~x) == 0
其次,~x 的 子集 y = ((~x) ^ (1<<k)), 0<= k < n(如果k这一位是1),y&x == 0
所以枚举 a[i] ,如果a[i]每被标记,搜索 (~a[i])的子集, 子集的子集......,边搜索边标记元素,如果出现一个y也属于a[i],那么再搜索(~y)的子集
这样就能保证一个联通图里的元素都能在一次搜索里被标记完
代码:
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 8e6 + ;
int a[N], tot, n;
bool hs[N], vis[N];
void dfs(int x) {
if(vis[x]) return ;
vis[x] = true;
if(hs[x]) dfs(tot ^ x);
for (int i = ; i < n; i++) {
if(x & (<<i)) dfs(x ^ (<<i));
}
}
int main() {
int m;
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++) scanf("%d", &a[i]), hs[a[i]] = true;
tot = (<<n) - ;
int ans = ;
for (int i = ; i <= m; i++) {
if(!vis[a[i]]) {
ans++;
vis[a[i]] = true;
dfs(tot ^ a[i]);
}
}
printf("%d\n", ans);
return ;
}
Codeforces 987 F - AND Graph的更多相关文章
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces 987 K预处理BFS 3n,7n+1随机结论题/不动点逆序对 X&Y=0连边DFS求连通块数目
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- codeforces 21D:Traveling Graph
Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...
- Codeforces 459E Pashmak and Graph
http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...
随机推荐
- JDK常用命令(三)jmap
jmap jmap,Java Memory Map.主要用于打印指定Java进程(或核心文件.远程调试服务器)的共享对象内存映射或堆内存细节. jmap命令可以获得运行中的jvm的堆的快照,从而可以离 ...
- ORA-38301:can not perform DDL/DML Over Object in Recycle Bin 11.2.0.4
我们最近有两台测试服务器在oci direct load期间出现下列异常: 从表象上看,是我们在对表执行ddl操作,确实内部也是用了truncate table XXX,可是这个XXX并不是回收站里面 ...
- 碎碎念android eMMC【转】
本文转载自:https://blog.csdn.net/Fybon/article/details/44242549 一./dev/blockroot@:/dev/block #ls bootdevi ...
- LightOJ 1027 A Dangerous Maze(期望)题解
题意:n扇门,每扇门后都有一个值x,如果x<0会让你等待-x再重新回到这里选择门,x>0你经过x时间就会被传送走,问你被传送走的期望 思路:假设被传送走的期望为E,那么对于x<0来说 ...
- discuz 不能上传头像提示can not write to the data/tmp folder
# discuz 不能上传头像提示can not write to the data/tmp folder 解释: disucz头像上传不成功,提示data/tmp目录没有写入权限,这里的data/t ...
- website for .Net Core
5 Ways to Build Routing in ASP.NET Core Bundling in .NET Core MVC Applications with BundlerMinifier. ...
- 正则匹配-URL-域名
DNS规定,域名中的标号都由英文字母和数字组成,每一个标号不超过63个字符,也不区分大小写字母.标号中除连字符(-)外不能使用其他的标点符号.级别最低的域名写在最左边,而级别最高的域名写在最右边.由多 ...
- QT中QLineEdit的editingFinished()信号在按下回车时会触发两次的解决办法
class MyLineEdit : public QLineEdit { Q_OBJECT public: MyLineEdit(QWidget * parent = 0) : QLineEdit( ...
- algorithm.sty not found error in LaTeX 解决方法
参考: algorithm.sty not found error in LaTeX algorithm.sty not found error in LaTeX 解决方法 错误日志: LaTeX E ...
- A Simple Note on "P4FPGA: A Rapid Prototyping Framework for P4"
论文:P4FPGA: A Rapid Prototyping Framework for P4 Github:https://github.com/p4fpga Reference: Han Wang ...