zoukankan      html  css  js  c++  java
  • 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
    ");
    	else printf("YES
    ");
    	return 0;
    }
    

      

  • 相关阅读:
    数据库设计 一对多 多对多 无限级菜单 设计方法
    线程创建的四种方式
    Linux常用指令---grep(搜索过滤)
    Netty4实战
    linux查找日志技巧
    javascript---关于字符串和数组的方法
    angularJs---route
    JavaScript---详解scroll
    JavaScript--location
    socket中 emit和on的写法
  • 原文地址:https://www.cnblogs.com/chadinblog/p/6437722.html
Copyright © 2011-2022 走看看