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存 ...
随机推荐
- listen()函数中backlog参数分析
实例分析1 将服务器端的listen函数backlog设置为2,用20个客户端与服务器建立连接,查看连接的建立情况. 服务器代码: #include <stdio.h> #include& ...
- android之csv导出
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 20145325张梓靖 《网络对抗技术》 MSF基础应用
20145325张梓靖 <网络对抗技术> MSF基础应用 实验内容 掌握metasploit的基本应用方式以及常用的三种攻击方式的思路 主动攻击,即对系统的攻击,不需要被攻击方配合,这里以 ...
- 尚硅谷面试第一季-07Spring Bean的作用域之间有什么区别
目录结构: 关键性代码: beans.xml <!-- ★bean的作用域 可以通过scope属性来指定bean的作用域 -singleton:默认值.当IOC容器一创建就会创建bean的实例, ...
- topcoder srm 688 div1 -3
1.给出一个只包含'(',')'的字符串$s$,现在对它进行若干次如下操作使其变成匹配的括号串(每次操作包含3个步骤):(1)选择 $L,R,L\leq R$;(2)将$L,R$之间的字符翻转:(3) ...
- centos6.8下如何升级git版本?
1. 安装开发环境 yum install -y curl curl-devel zlib-devel openssl-devel perl cpio expat-devel gettext-deve ...
- C Primer Plus 创建友好的输入界面 笔记
看代码 char inputFunCode; while(inputFunCode = getchar()){ '){ printf("you choose string conn\n&qu ...
- 如何使用jqueryUi的datepicker日历控件?
参考: http://www.jb51.net/article/85007.htm 这里的日历控件是, 基于jquery的jqureyui中的一个 widget. 需要js 文件: 外部的js文件, ...
- 集合05_Collections工具类
Collections工具类 提供大量方法用于操作集合,比如排序,查找,替换 同步控制 提供synchronizedXxx()方法将指定集合类包装成线程同步的集合. List<String> ...
- (转)awesome-text-summarization
awesome-text-summarization 2018-07-19 10:45:13 A curated list of resources dedicated to text summari ...