codeforces776D
这题的意思就是原本有一个长度为n的01串,再给出m的长度为n的01串,要求你判定是否可以通过原串与m个串中的某些串xor使得原串到达一个状态。n,m小于1e5。
这题最初我发现不可做,因为这貌似是个NPC问题/密码加密方法之一,只能暴力。
后来我发现我忽略了一个条件:n个01位中,m个串中只有两个串的位置为1,这就很资磁了,若选此串,必选彼串,这种限制条件就是2-SAT的经典应用,直接上2-SAT即可。
考完发现了神犇都是用二分图染色的变种做的,仔细想想,2-SAT只是将染色方案拆开看了,实质不变。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define LL long long
#define FILE "dealing"
#define up(i,j,n) for(int i=j;i<=n;i++)
int read(){
int x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
const int maxn=1600000,inf=1000000000;
bool cmin(int& a,int b){return a>b?a=b,true:false;}
bool cmax(int& a,int b){return a<b?a=b,true:false;}
int n,cnt[maxn];
struct node{int y,next;}e[maxn];int len=0,linkk[maxn];
void insert(int x,int y){e[++len].y=y;e[len].next=linkk[x];linkk[x]=len;}
int b[maxn][2],a[maxn],d[maxn];
bool c[maxn];bool flag=0;int q[maxn],top=0;
void dfs(int x){
if(((x%2)&&c[x+1])||(!(x%2)&&c[x-1])){flag=1;return;}
c[x]=1;q[++top]=x;
for(int i=linkk[x];i;i=e[i].next){
if(!c[e[i].y])dfs(e[i].y);
}
}
int m;
int solve(){
for(int i=1;i<=m<<1;i+=2){
if(!c[i]&&!c[i+1]){
top=0;
dfs(i);
if(flag){
while(top)c[q[top--]]=0;
flag=0;
dfs(i+1);
if(flag)return 1;
}
}
}
return 0;
}
int main(){
//freopen(FILE".in","r",stdin);
// freopen(FILE".out","w",stdout);
n=read();m=read();
up(i,1,n)a[i]=read();
up(i,1,m){
cnt[i]=read();
up(j,1,cnt[i]){
int y=read();
if(!b[y][0])b[y][0]=i;
else b[y][1]=i;
}
}
up(i,1,n){
if(a[i])insert(b[i][0]<<1,b[i][1]<<1),insert(b[i][1]<<1,b[i][0]<<1),insert((b[i][0]<<1)-1,(b[i][1]<<1)-1),insert((b[i][1]<<1)-1,(b[i][0]<<1)-1);
else insert((b[i][0]<<1)-1,(b[i][1]<<1)),insert((b[i][1]<<1)-1,(b[i][0]<<1)),insert((b[i][0]<<1),(b[i][1]<<1)-1),insert(b[i][1]<<1,(b[i][0]<<1)-1);
}
if(solve())printf("NO\n");
else printf("YES\n");
return 0;
}
codeforces776D的更多相关文章
- codeforces776D The Door Problem
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
随机推荐
- Codeforces 946 C.String Transformation
C. String Transformation time limit per test 1 second memory limit per test 256 megabytes input st ...
- null相关
本文转自:http://www.cnblogs.com/peida/archive/2013/06/14/Guava_Optional.html null代表不确定的对象: Java中,null是一个 ...
- 洛谷——P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would li ...
- Linux Root下的.gvfs出现异常解决办法(导致source失败,自启动失败)
原文地址: http://www.cnblogs.com/tdyizhen1314/p/4142991.html 在linux系统下安装软件或复制文件的时候,复制不成功,出现错误如下: error ...
- android wifi state and wifi ap state
/** * Wi-Fi is currently being disabled. The state will change to {@link #WIFI_STATE_DISABLED} if * ...
- react-redux 和 redux-saga 小结
react-redux 将 store 绑定到 props 上,便于全局调用. redux-saga 是将 redux 的同步转换为异步. 注: dispatch 到 saga , saga 匹配行为 ...
- 广告制胜无它,顺应人性尔——leo鉴书63
近期看了几本怎样写文案的书.对广告有了些兴趣.查了下相关销量排行,位置比較高的是本叫<科学的广告+我的广告生涯>的书,是同一作者(Claude C. Hopkins)两本书的合集.前者是他 ...
- 【java读书笔记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
两个月之前准备软考时,简单的从理论上总结了最经常使用的数据结构和算法,比方:线性表,链表,图.在进行java开发时,jdk为我们提供了一系列对应的类来实现主要的数据结构.jdk所提供的容器API位于j ...
- selenium之 文件上传方法
文件上传是所有UI自动化测试都要面对的一个头疼问题 首先,我们要区分出上传按钮的种类,大体上可以分为两种,一种是input框,另外一种就比较复杂,通过js.flash等实现,标签非input 我们分别 ...
- 显示和隐藏Mac隐藏文件的命令
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令:defaults writ ...