#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。
*/
codeforces Round 400 D-The Door Problem
最新推荐文章于 2021-11-02 19:36:32 发布