zoukankan      html  css  js  c++  java
  • CodeForces

    Discription

    Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are m switches. Each switch control doors of some rooms, but each door is controlled by exactly two switches.

    You are given the initial configuration of the doors. Toggling any switch, that is, turning it ON when it is OFF, or turning it OFF when it is ON, toggles the condition of the doors that this switch controls. Say, we toggled switch 1, which was connected to room 1, 2 and 3 which were respectively locked, unlocked and unlocked. Then, after toggling the switch, they become unlocked, locked and locked.

    You need to tell Sherlock, if there exists a way to unlock all doors at the same time.

    Input

    First line of input contains two integers n and m (2 ≤ n ≤ 1052 ≤ m ≤ 105) — the number of rooms and the number of switches.

    Next line contains n space-separated integers r1, r2, ..., rn (0 ≤ ri ≤ 1) which tell the status of room doors. The i-th room is locked if ri = 0, otherwise it is unlocked.

    The i-th of next m lines contains an integer xi (0 ≤ xi ≤ n) followed by xi distinct integers separated by space, denoting the number of rooms controlled by the i-th switch followed by the room numbers that this switch controls. It is guaranteed that the room numbers are in the range from 1 to n. It is guaranteed that each door is controlled by exactly two switches.

    Output

    Output "YES" without quotes, if it is possible to open all doors at the same time, otherwise output "NO" without quotes.

    Examples

    Input
    3 3
    1 0 1
    2 1 3
    2 1 2
    2 2 3
    Output
    NO
    Input
    3 3
    1 0 1
    3 1 2 3
    1 2
    2 1 3
    Output
    YES
    Input
    3 3
    1 0 1
    3 1 2 3
    2 1 2
    1 3
    Output
    NO

    Note

    In the second example input, the initial statuses of the doors are [1, 0, 1] (0 means locked, 1 — unlocked).

    After toggling switch 3, we get [0, 0, 0] that means all doors are locked.

    Then, after toggling switch 1, we get [1, 1, 1] that means all doors are unlocked.

    It can be seen that for the first and for the third example inputs it is not possible to make all doors unlocked.

       推了一小下发现事two-sat的模板题23333

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<cctype>
    #include<vector>
    #define ll long long
    using namespace std;
    const int maxn=100005;
    #define pb push_back
    
    inline int read(){
    	int x=0; char ch=getchar();
    	for(;!isdigit(ch);ch=getchar());
    	for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
    	return x;
    }
    
    int n,m,N[maxn][2],k,now,num,C[maxn*2];
    bool zt[maxn],mark[maxn*2];
    
    vector<int> g[maxn*2];
    
    inline void add(int x,int y,int z){
    	if(z) g[x*2].pb(y*2),g[y*2].pb(x*2),g[x*2+1].pb(y*2+1),g[y*2+1].pb(x*2+1);
    	else g[x*2].pb(y*2+1),g[y*2+1].pb(x*2),g[x*2+1].pb(y*2),g[y*2].pb(x*2+1);
    }
    
    bool dfs(int x){
    	if(mark[x^1]) return 0;
    	if(mark[x]) return 1;
    	
    	mark[x]=1,C[++num]=x;
    	
    	for(int i=g[x].size()-1;i>=0;i--) if(!dfs(g[x][i])) return 0;
    	return 1;
    }
    
    inline bool solve(){
    	for(int i=1;i<=n;i++) add(N[i][0],N[i][1],zt[i]);
    	
    	for(int i=(m<<1);i;i-=2) if(!mark[i]&&!mark[i+1])
    		if(!dfs(i)){
    			while(num) mark[C[num--]]=0;
    			if(!dfs(i+1)) return 0;
    		}
    	
    	return 1;
    }
    
    int main(){
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;i++) zt[i]=read();
    	for(int i=1;i<=m;i++){
    		k=read();
    		while(k--){
    			now=read();
    			if(!N[now][0]) N[now][0]=i;
    			else N[now][1]=i;
    		}
    	}
    	
        if(solve()) puts("YES");
        else puts("NO");
        
        return 0;
    }
    

      

  • 相关阅读:
    python常用运维脚本实例
    数据库优化方案整理
    Python第一章概述与环境安装
    九阴真经
    常用的kubectl命令
    第7篇管理存储资源
    第8篇NFS PersistentVolume
    第10篇用 ConfigMap 管理配置
    第11篇Kubernetes部署微服务电商平台
    第12篇Kubernetes 监控
  • 原文地址:https://www.cnblogs.com/JYYHH/p/9163700.html
Copyright © 2011-2022 走看看