传送门

这题的意思就是原本有一个长度为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的更多相关文章

  1. codeforces776D The Door Problem

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

随机推荐

  1. [BZOJ5286][洛谷P4425][HNOI2018]转盘(线段树)

    5286: [Hnoi2018]转盘 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 15  Solved: 11[Submit][Status][Di ...

  2. 提高速度 history 的利用

    history的介绍history是shell的内置命令,其内容在系统默认的shell的man手册中.history是显示在终端输入并执行的过命令,系统默认保留1000条.[root@localhos ...

  3. Play框架的用户验证。

    最近刚刚参与一个基于Play框架的管理平台的升级工作,其中涉及到了用户的验证工作.第一次接触play框架,直接看已有代码,有点晕.因此,自己实现了一个简单的用户验证功能. 首先,新建一个User类,包 ...

  4. SQL Server 存储

    http://baoqiangwang.blog.51cto.com/1554549/541298/

  5. Spring 与 MyBatis 整合

    一.实验介绍 1.1 实验内容 本节课程将整合 Spring 和 MyBatis,并采用 Junit 进行单元测试. 1.2 实验知识点 Spring - MyBatis 整合 Junit 单元测试 ...

  6. js利用offsetWidth和clientWidth来计算滚动条的宽度

    原文: http://www.haorooms.com/post/js_scroll_width 参考: https://www.cnblogs.com/benxiaohai-microcosm/p/ ...

  7. PS 如何制作柔和的边框

    柔和边框制作 1 新建一个图层,按填充为黑色.(填充当前图层为前景色 A/T+Delete 背景色 CTRL+Delete),不透明度设为60%.混合模式为正片叠底,   2 选择椭圆选择一片区域(也 ...

  8. NATSserver配置具体解释

    NATSserver配置具体解释 作者:chszs,未经博主同意不得转载. 经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 虽然NATS能够无配置的执行,但也能 ...

  9. jsp中获取spring 管理的bean(通过config)

    WebApplicationContext wac = (WebApplicationContext)config.getServletContext().getAttribute(WebApplic ...

  10. 基于OpenCL的深度学习工具:AMD MLP及其使用详解

    基于OpenCL的深度学习工具:AMD MLP及其使用详解 http://www.csdn.net/article/2015-08-05/2825390 发表于2015-08-05 16:33| 59 ...