#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define eps 1e-2
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x)&(-x)
#define bug printf("!!!!!\n");
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
typedef long double LD;
typedef pair<int,int> pii;
typedef unsigned long long uLL;
const int N = 2e5+2;
const int INF = 1<<30;
const int mod = 1e9+7;
int n,m;
int sta[N],fa[N],cnt[N],bl[2][N];
int find(int x){
if(fa[x]==x) return x;
else return fa[x]=find(fa[x]);
}
void Union(int x,int y){
fa[find(x)]=find(y);
}
void solve(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&sta[i]);
for(int i=1;i<=m;i++){
int k;scanf("%d",&k);
for(int j=1;j<=k;j++){
int x;scanf("%d",&x);bl[cnt[x]++][x]=i;
}
fa[i]=i;fa[i+m]=i+m;
}
for(int i=1;i<=n;i++){
if(sta[i]==0){
Union(bl[0][i]+m,bl[1][i]);
Union(bl[0][i],bl[1][i]+m);
}else{
Union(bl[0][i],bl[1][i]);
Union(bl[0][i]+m,bl[1][i]+m);
}
}
for(int i=1;i<=m;i++){
if(find(i)==find(i+m)){
cout<<"NO"<<endl;return ;
}
}
cout<<"YES"<<endl;
return ;
return;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// ios::sync_with_stdio(false);
int t = 1;
//scanf("%d",&t);
while(t--){
// printf("Case %d: ",cas++);
solve();
}
return 0;
}
ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 扩展域并查集
于 2022-05-21 14:49:39 首次发布