codeforces Round 400 D-The Door Problem

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N=100000+10;
const LL INF=1000000000000000LL;

vector<int>a[N];
int r[N];
int n,m;
int bcj[N],s[N];
int find(int x){
    if (bcj[x]==x) return x;
    int fx=find(bcj[x]);
    s[x] = ( s[x] + s[ bcj[x] ] )%2;
    return bcj[x]=fx;
}
int Union(int x,int y,int z){
	int fx=find(x);
    int fy=find(y);
	bcj[fx]=fy;
	s[fx]=(z-s[x]-s[y]+2)%2;
}
int main()
{
    //-freopen("1.txt","r",stdin);
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)scanf("%d",&r[i]);//1 open ; 0 lock
	for (int i=1;i<=m;i++){
		int x;scanf("%d",&x);
		for (int j=0;j<x;j++){
			int y;scanf("%d",&y);
			a[y].push_back(i);
		}
	}
	for (int i=1;i<=m;i++)bcj[i]=i,s[i]=0;
	for (int i=1;i<=n;i++){
		if (find(a[i][0])==find(a[i][1])){
            if ((s[ a[i][0] ]+s[ a[i][1] ])%2!=1-r[i]){
                puts("NO");
                return 0;
            }
		}else Union(a[i][0],a[i][1],1-r[i]);
	}
	puts("YES");
	//system("pause");
	return 0;
}
/*
题目: http://codeforces.com/contest/776/problem/D
题意: n个房间,有些门是开的(1),有些门是关的(0);现有m个开关,可以控制xi个房间。每个房间恰有两个开关控制。问是否能使所有门都开。(n≤10^5,m≤10^5)

题解: 将开关看成点,房间看成边(连接两个开关),若房间开着则边权为1,否则为0。对点用0-1染色,边权为1的点颜色相同,边权为0的颜色相反。若能染遍所有点,则可行。
       或者:同上建图,采用并查集求解,s[x]表示x与父亲的关系,相同为0,不同为1。
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值