Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem(二分染色?/2-sat,好题)

D. The Door Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 12 and 3which 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.


题意:

有n个门,给出它们的状态0/1(0-关,1-开)。

给出m个开关,每个开关控制若干门,按一次开关可以将其控制的门的状态改变,输入保证每个门最多被两个开关控制。

问能不能讲所有的门打开。



题解:

其实,这一题和这个题是一样的做法,将开关看作点,若一个门被开关v1,v2控制,则连一条边,边的颜色就是门最初的状态,然后就和那一题完全一样了。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=1e5+100;
int head[maxn];
struct edge
{
    int to,next,c;
}e[maxn*2];   //
int tol=0;
void add(int u,int v,int c)
{
    e[++tol].to=v,e[tol].next=head[u],e[tol].c=c,head[u]=tol;
}
int a[maxn];
VI V[maxn];
int vis[maxn];
bool dfs(int u,int p)
{
    if(vis[u]) return vis[u]==p;
    vis[u]=p;
    for(int i=head[u];i;i=e[i].next)
    {
        if(!dfs(e[i].to,e[i].c? p:p^3))
            return false;
    }
    return true;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    rep(i,1,n+1) scanf("%d",&a[i]);
    rep(i,1,m+1)
    {
        int v;
        scanf("%d",&v);
        while(v--)
        {
            int u;
            scanf("%d",&u);
            if(V[u].size()!=0)
                add(i,V[u][0],a[u]),add(V[u][0],i,a[u]);
            else V[u].pb(i);
        }
    }
    memset(vis,0,sizeof(vis));
    rep(i,1,m+1)
    {
        if(!vis[i])
        {
            if(!dfs(i,1)) return 0*puts("NO");
        }
    }
    puts("YES");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值