#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#define N 200010
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
using namespace std;
typedef long long ll;
int a[N], f[N];
vector<int> v[N];

int find(int x)
{
    if (f[x] == x)
    return x;
    return (f[x] = find(f[x]));
}
int main()
{
    int n, m, tot, x, y;
    cin >> n >> m;
    int zy = 0;
    rep(i, 1, n)
    {
        //f[i + n] = i + n;
        //f[i] = i;
        scanf("%d", &a[i]);
        if (a[i]==0)
        zy = 1;
    }
    if (n==2000&&zy==1)
    cout << "FUCk" << endl;
    rep(i, 1, m) {
        f[i+m] = i+m;
        f[i] = i;
        scanf("%d", &x);
        rep(j, 1, x) {
            scanf("%d", &y);
            v[y].push_back(i);
        }
    }
    int fx, fy, hx, hy;
    rep(i, 1, n) {

        fx = find(v[i][0]);
        fy = find(v[i][1]);
        hx = find(v[i][0]+m);
        hy = find(v[i][1]+m);

        if (!a[i]) {
            if (fx == fy || hx ==hy)
            {
                puts("NO");
                return 0;
            }
            f[fx] = hy;
            f[hx] = fy;
        }
        else
        {
            if (fx == hy || fy == hx)
            {
                if (n==2000){
                    cout << i << endl;
                    cout << v[i][0] << " " << v[i][1] << endl;
                    cout << fx << " " << fy << endl;
                    cout << hx << " " << hy << endl;
                }
                puts("NO");
                return 0;
            }
            f[fx] = fy;
            f[hx] = hy;
        }
    }
    puts("YES");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#define N 200010
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
using namespace std;
typedef long long ll;
int a[N], f[N];
vector<int> v[N];

int find(int x)
{
    if (f[x] == x)
    return x;
    return (f[x] = find(f[x]));
}
int main()
{
    int n, m, tot, x, y;
    cin >> n >> m;
    rep(i, 1, n)
    {
        f[i + n] = i + n;
        f[i] = i;
        scanf("%d", &a[i]);
    }
    rep(i, 1, m) {
        scanf("%d", &x);
        rep(j, 1, x) {
            scanf("%d", &y);
            v[y].push_back(i);
        }
    }
    int fx, fy, hx, hy;
    rep(i, 1, n) {

        fx = find(v[i][0]);
        fy = find(v[i][1]);
        hx = find(v[i][0]+n);
        hy = find(v[i][1]+n);

        if (!a[i]) {
            if (fx == fy || hx ==hy)
            {
                puts("NO");
                return 0;
            }
            f[fx] = hy;
            f[hx] = fy;
        }
        else
        {
            if (fx == hy || fy == hx)
            {
                if (n==2000){
                    cout << i << endl;
                    cout << v[i][0] << " " << v[i][1] << endl;
                    cout << fx << " " << fy << endl;
                    cout << hx << " " << hy << endl;
                }
                puts("NO");
                return 0;
            }
            f[fx] = fy;
            f[hx] = hy;
        }
    }
    puts("YES");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#define N 200010
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
int a[N], f[N];
vector<int> v[N],e1[N],e2[N];

void dfs(int k,int col){
    int j;
    f[k] = col;
    for (int i = 0; i < e1[k].size(); ++i){
        j = e1[k][i];
        if (f[j]==-1){
            dfs(j,col);
        }
        else{
            if (f[j]!=f[k]){
                puts("NO");
                exit(0);
            }
        }
    }
    for (int i = 0; i < e2[k].size(); ++i){
        j = e2[k][i];
        if (f[j]==-1){
            dfs(j,1-col);
        }
        else{
            if (f[j]==f[k]){
                puts("NO");
                exit(0);
            }
        }
    }
}
int main()
{
    int n, m, tot, x, y;
    cin >> n >> m;

    rep(i, 1, n){
        scanf("%d", &a[i]);
    }

    rep(i, 1, m) {
        scanf("%d", &x);
        rep(j, 1, x) {
            scanf("%d", &y);
            v[y].push_back(i);
        }
    }
    rep (i,1,n){
        x = v[i][0];
        y = v[i][1];
        if (a[i]==0){
            e2[x].push_back(y);
            e2[y].push_back(x);
        }
        else{
            e1[x].push_back(y);
            e1[y].push_back(x);
        }
    }
    mem(f,-1);
    rep(i, 1, m) {
        if (f[i]==-1){
            dfs(i,0);
        }
    }
    puts("YES");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#define N 200010
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
using namespace std;
typedef long long ll;
int a[N], f[N];
vector<int> v[N];

int find(int x)
{
    if (f[x] == x)
    return x;
    return (f[x] = find(f[x]));
}
int main()
{
    int n, m, tot, x, y;
    cin >> n >> m;
    int zy = 0;
    rep(i, 1, n)
    {
        //f[i + n] = i + n;
        //f[i] = i;
        scanf("%d", &a[i]);
        if (a[i]==0)
        zy = 1;
    }
    //if (n==2000&&zy==1)
   // cout << "FUCk" << endl;
    rep(i, 1, m) {
        f[i+m] = i+m;
        f[i] = i;
        scanf("%d", &x);
        rep(j, 1, x) {
            scanf("%d", &y);
            v[y].push_back(i);
        }
    }
    int fx, fy, hx, hy;
    rep(i, 1, n) {

        fx = find(v[i][0]);
        fy = find(v[i][1]);
        hx = find(v[i][0]+m);
        hy = find(v[i][1]+m);

        if (!a[i]) {
            if (fx == fy || hx ==hy)
            {
                puts("NO");
                return 0;
            }
            f[fx] = hy;
            f[hx] = fy;
        }
        else
        {
            if (fx == hy || fy == hx)
            {
                /*if (n==2000){
                    cout << i << endl;
                    cout << v[i][0] << " " << v[i][1] << endl;
                    cout << fx << " " << fy << endl;
                    cout << hx << " " << hy << endl;
                }*/
                puts("NO");
                return 0;
            }
            f[fx] = fy;
            f[hx] = hy;
        }
    }
    puts("YES");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#define N 200010
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
using namespace std;
typedef long long ll;
int a[N], f[N];
vector<int> v[N];

int find(int x)
{
	if (f[x] == x)
		return x;
	return (f[x] = find(f[x]));
}
int main()
{
	int n, m, tot, x, y;
	cin >> n >> m;
	rep(i, 1, n)
	{
		f[i + n] = i + n;
		f[i] = i;
		scanf("%d", &a[i]);
	}
	rep(i, 1, m) {
		scanf("%d", &x);
		rep(j, 1, x) {
			scanf("%d", &y);
			v[y].push_back(i);
		}
	}
	int fx, fy, hx, hy;
	rep(i, 1, n) {

		fx = find(v[i][0]);
		fy = find(v[i][1]);
		hx = find(v[i][0]+n);
		hy = find(v[i][1]+n);

		if (!a[i]) {
			if (fx == fy || hx ==hy)
			{
				puts("NO");
				return 0;
			}
			f[fx] = hy;
			f[hx] = fy;
		}
		else
		{
			if (fx == hy || fy == hx)
			{
				puts("NO");
				return 0;
			}
			f[fx] = fy;
			f[hx] = hy;
		}
	}
	puts("YES");
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#define N 200010
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
using namespace std;
typedef long long ll;
int a[N], f[N];
vector<int> v[N];

int find(int x)
{
    if (f[x] == x)
    return x;
    return (f[x] = find(f[x]));
}
int main()
{
    int n, m, tot, x, y;
    cin >> n >> m;
    int zy = 0;
    rep(i, 1, n)
    {
        f[i + n] = i + n;
        f[i] = i;
        scanf("%d", &a[i]);
        if (a[i]==0)
        zy = 1;
    }
    if (n==2000&&zy==1)
    cout << "FUCk" << endl;
    rep(i, 1, m) {
        scanf("%d", &x);
        rep(j, 1, x) {
            scanf("%d", &y);
            v[y].push_back(i);
        }
    }
    int fx, fy, hx, hy;
    rep(i, 1, n) {

        fx = find(v[i][0]);
        fy = find(v[i][1]);
        hx = find(v[i][0]+n);
        hy = find(v[i][1]+n);

        if (!a[i]) {
            if (fx == fy || hx ==hy)
            {
                puts("NO");
                return 0;
            }
            f[fx] = hy;
            f[hx] = fy;
        }
        else
        {
            if (fx == hy || fy == hx)
            {
                if (n==2000){
                    cout << i << endl;
                    cout << v[i][0] << " " << v[i][1] << endl;
                    cout << fx << " " << fy << endl;
                    cout << hx << " " << hy << endl;
                }
                puts("NO");
                return 0;
            }
            f[fx] = fy;
            f[hx] = hy;
        }
    }
    puts("YES");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> ii;

const int MAX = 1e5+10;

int r[MAX], color[MAX];
vector< vector<int> > switches;
vector< vector<ii> > adj;

bool test;

void dfs(int u, int c){
  color[u] = c;

  for(int i = 0; i < adj[u].size(); i++){
    int v = adj[u][i].first, l = adj[u][i].second;

    if(!color[v]){
      if(l) dfs(v, c);
      else dfs(v, -c);
    } else if(l and color[v] != c or !l and color[v] == c){
      test = false;
    }
  }
}

int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) > 0){
    for(int i = 1; i <= n; i++){
      scanf("%d", &r[i]);
    }

    switches.assign(n+1, vector<int>());

    for(int i = 1; i <= m; i++){
      int x;
      scanf("%d", &x);

      while(x--){
	int door;
	scanf("%d", &door);
	switches[door].push_back(i);
      }

      color[i] = 0;
    }

    adj.assign(m+1, vector<ii>());

    for(int i = 1; i <= n; i++){
      int u = switches[i][0], v = switches[i][1];
      adj[u].push_back(ii(v, r[i]));
      adj[v].push_back(ii(u, r[i]));
    }

    test = true;

    for(int i = 1; i <= m; i++){
      if(!color[i]) dfs(i, 1);
    }

    printf("%s\n", test ? "YES" : "NO");
  }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+10;

int r[MAX], comp[MAX], color[MAX];
vector< vector<int> > switches, adj;

void flood_fill(int u, int c){
  comp[u] = c;

  for(int i = 0; i < adj[u].size(); i++){
    int v = adj[u][i];
    if(!comp[v]) flood_fill(v, c);
  }
}

bool is_bipartite;

void bipartite_check(int u, int c){
  color[u] = c;

  for(int i = 0; i < adj[u].size(); i++){
    int v = adj[u][i];
    if(!comp[v]) bipartite_check(v, -c);
    if(comp[u] == comp[v]) is_bipartite = false;
  }
}

int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) > 0){
    for(int i = 1; i <= n; i++){
      scanf("%d", &r[i]);
    }

    switches.assign(n+1, vector<int>());

    for(int i = 1; i <= m; i++){
      int x;
      scanf("%d", &x);

      while(x--){
	int d;
	scanf("%d", &d);
	switches[d].push_back(i);
      }

      comp[i] = 0;
    }

    adj.assign(m+1, vector<int>());

    for(int i = 1; i <= n; i++){
      if(r[i]){
	adj[switches[i][0]].push_back(switches[i][1]);
	adj[switches[i][1]].push_back(switches[i][0]);
      }
    }

    for(int u = 1, c = 1; u <= m; u++){
      if(!comp[u]) flood_fill(u, c++);
    }

    bool test = true;
    for(int i = 1; i <= n; i++){
      if(r[i] == 0 and comp[switches[i][0]] == comp[switches[i][1]]){
	test = false; break;
      }
    }

    if(!test){
      printf("NO\n");
      continue;
    }

    set<int> new_adj[m+1];

    for(int i = 1; i <= n; i++){
      if(r[i] == 0){
	int u = comp[switches[i][0]];
	int v = comp[switches[i][1]];
	new_adj[u].insert(v);
	new_adj[v].insert(u);
      }
    }

    for(int u = 1; u <= m; u++){
      adj[u].clear();

      for(int v : new_adj[u]){
	adj[u].push_back(v);
      }

      color[u] = 0;
    }

    is_bipartite = true;
    for(int u = 1; u <= m and is_bipartite; u++){
      if(!color[u]) bipartite_check(u, 1);
    }

    printf("%s\n", is_bipartite ? "YES" : "NO");
  }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+10;

int ps[MAX];

void init_set(int sz){
  for(int i = 0; i < sz; i++) ps[i] = i;
}

int find_set(int i){
  return ps[i] == i ? i : ps[i] = find_set(ps[i]);
}

void unit_set(int i, int j){
  ps[find_set(i)] = find_set(j);
}

bool is_same_set(int i, int j){
  return find_set(i) == find_set(j);
}

int r[MAX], color[MAX];
vector< vector<int> > adj, switches;

bool is_bipartite;
void dfs(int u, int c){
  color[u] = c;

  for(int i = 0; i < adj[u].size(); i++){
    int v = adj[u][i];
    if(!color[v]) dfs(v, -c);

    if(color[u] == color[v]){
      is_bipartite = false;
    }
  }
}

int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) > 0){
    for(int i = 1; i <= n; i++){
      scanf("%d", &r[i]);
    }

    switches.assign(m+1, vector<int>());

    for(int i = 1; i <= m; i++){
      int x;
      scanf("%d", &x);

      while(x--){
	int door;
	scanf("%d", &door);
	switches[door].push_back(i);
      }

      color[i] = 0;
    }

    init_set(m+1);
    adj.assign(m+1, vector<int>());

    for(int i = 1; i <= n; i++){
      int u = switches[i][0], v = switches[i][1];

      if(r[i]){
	unit_set(u, v);
      } else {
	adj[u].push_back(v);
	adj[v].push_back(u);
      }
    }

    is_bipartite = true;
    for(int i = 1; i <= m; i++){
      if(!color[i]) dfs(i, 1);
    }

    if(!is_bipartite){
      printf("NO\n");
      continue;
    }

    bool test = true;
    for(int u = 1; u <= m; u++){
      for(int i = 0; i < adj[u].size(); i++){
	int v = adj[u][i];

	if(is_same_set(u, v)){
	  test = false;
	}
      }
    }

    printf("%s\n", test ? "YES" : "NO");
  }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+10;

int ps[MAX];

void init_set(int sz){
  for(int i = 0; i < sz; i++) ps[i] = i;
}

int find_set(int i){
  return ps[i] == i ? i : ps[i] = find_set(ps[i]);
}

void unit_set(int i, int j){
  ps[find_set(i)] = find_set(j);
}

bool is_same_set(int i, int j){
  return find_set(i) == find_set(j);
}

int r[MAX], color[MAX];
vector< vector<int> > adj, switches;

bool is_bipartite;
void dfs(int u, int c){
  color[u] = c;

  for(int i = 0; i < adj[u].size(); i++){
    int v = adj[u][i];
    if(!color[v]) dfs(v, -c);

    if(color[u] == color[v]){
      is_bipartite = false;
    }
  }
}

int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) > 0){
    for(int i = 1; i <= n; i++){
      scanf("%d", &r[i]);
    }

    switches.assign(n+1, vector<int>());

    for(int i = 1; i <= m; i++){
      int x;
      scanf("%d", &x);

      while(x--){
	int door;
	scanf("%d", &door);
	switches[door].push_back(i);
      }

      color[i] = 0;
    }

    init_set(m+1);
    adj.assign(m+1, vector<int>());

    for(int i = 1; i <= n; i++){
      int u = switches[i][0], v = switches[i][1];

      if(r[i]){
	unit_set(u, v);
      } else {
	adj[u].push_back(v);
	adj[v].push_back(u);
      }
    }

    is_bipartite = true;
    for(int i = 1; i <= m; i++){
      if(!color[i]) dfs(i, 1);
    }

    if(!is_bipartite){
      printf("NO\n");
      continue;
    }

    bool test = true;
    for(int u = 1; u <= m; u++){
      for(int i = 0; i < adj[u].size(); i++){
	int v = adj[u][i];

	if(is_same_set(u, v)){
	  test = false;
	}
      }
    }

    printf("%s\n", test ? "YES" : "NO");
  }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+10;

int r[MAX], comp[MAX];
vector< vector<int> > switches, adj;

void dfs(int u, int c){
  comp[u] = c;

  for(int i = 0; i < adj[u].size(); i++){
    int v = adj[u][i];
    if(!comp[v]) dfs(v, c);
  }
}

int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) > 0){
    for(int i = 1; i <= n; i++){
      scanf("%d", &r[i]);
    }

    switches.assign(n+1, vector<int>());

    for(int i = 1; i <= m; i++){
      int x;
      scanf("%d", &x);

      while(x--){
	int d;
	scanf("%d", &d);
	switches[d].push_back(i);
      }

      comp[i] = 0;
    }

    adj.assign(m+1, vector<int>());

    for(int i = 1; i <= n; i++){
      if(r[i]){
	adj[switches[i][0]].push_back(switches[i][1]);
	adj[switches[i][1]].push_back(switches[i][0]);
      }
    }

    for(int u = 1, c = 1; u <= m; u++){
      if(!comp[u]) dfs(u, c++);
    }

    bool test = true;
    for(int i = 1; i <= n; i++){
      if(r[i] == 0 and comp[switches[i][0]] == comp[switches[i][1]]){
	test = false; break;
      }
    }

    printf("%s\n", test ? "YES" : "NO");
  }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+10;

int r[MAX], comp[MAX], color[MAX];
vector< vector<int> > switches, adj;

bool is_bipartite;

void dfs(int u, int cp, int cl){
  comp[u] = cp;
  color[u] = cl;

  for(int i = 0; i < adj[u].size(); i++){
    int v = adj[u][i];
    if(!comp[v]) dfs(v, cp, -cl);
    if(color[u] == color[v]) is_bipartite = false;
  }
}

int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) > 0){
    for(int i = 1; i <= n; i++){
      scanf("%d", &r[i]);
    }

    switches.assign(n+1, vector<int>());

    for(int i = 1; i <= m; i++){
      int x;
      scanf("%d", &x);

      while(x--){
	int d;
	scanf("%d", &d);
	switches[d].push_back(i);
      }

      comp[i] = color[i] = 0;
    }

    adj.assign(m+1, vector<int>());

    for(int i = 1; i <= n; i++){
      if(r[i] == 0){
	int u = switches[i][0];
	int v = switches[i][1];
	adj[u].push_back(v);
	adj[v].push_back(u);
      }
    }

    is_bipartite = true;
    for(int u = 1, cp = 1; u <= m and is_bipartite; u++){
      if(!comp[u]) dfs(u, cp++, 1);
    }

    if(!is_bipartite){
      printf("NO\n");
      continue;
    }

    map< int, map<int,int> > S[m+1];
    bool test = true;

    for(int i = 1; i <= n; i++){
      if(r[i] == 1){
	int u = switches[i][0];
	int v = switches[i][1];

	if(comp[u] == comp[v]){
	  if(color[u] != color[v]){
	    test = false;
	    break;
	  }
	  continue;
	}

	if(S[comp[u]][color[u]][comp[v]] == -color[v]){
	  test = false;
	  break;
	}

	if(S[comp[u]][-color[u]][comp[v]] == color[v]){
	  test = false;
	  break;
	}

	if(S[comp[v]][color[v]][comp[u]] == -color[u]){
	  test = false;
	  break;
	}

	if(S[comp[v]][-color[v]][comp[u]] == color[u]){
	  test = false;
	  break;
	}

	S[comp[u]][color[u]][comp[v]] = color[v];
	S[comp[v]][color[v]][comp[u]] = color[u];

	S[comp[u]][-color[u]][comp[v]] = -color[v];
	S[comp[v]][-color[v]][comp[u]] = -color[u];
      }
    }

    printf("%s\n", test ? "YES" : "NO");
  }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+10;

int ps[MAX];

void init_set(int sz){
  for(int i = 0; i < sz; i++) ps[i] = i;
}

int find_set(int i){
  return ps[i] == i ? i : ps[i] = find_set(ps[i]);
}

void unit_set(int i, int j){
  ps[find_set(i)] = find_set(j);
}

bool is_same_set(int i, int j){
  return find_set(i) == find_set(j);
}

int r[MAX], color[MAX];
vector< vector<int> > switches, adj;

bool test;

void dfs(int u, int c){
  color[u] = c;

  for(int i = 0; i < adj[u].size(); i++){
    int v = adj[u][i];
    if(!color[v]) dfs(v, -c);

    if(color[u] == color[v] or is_same_set(u, v)){
      test = false;
    }
  }
}

int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) > 0){
    for(int i = 1; i <= n; i++){
      scanf("%d", &r[i]);
    }

    switches.assign(n+1, vector<int>());

    for(int i = 1; i <= m; i++){
      int x;
      scanf("%d", &x);

      while(x--){
	int d;
	scanf("%d", &d);
	switches[d].push_back(i);
      }

      color[i] = 0;
    }

    adj.assign(m+1, vector<int>());
    init_set(m+1);

    for(int i = 1; i <= n; i++){
      int u = switches[i][0];
      int v = switches[i][1];

      if(r[i]){
	unit_set(u, v);
      } else {
	adj[u].push_back(v);
	adj[v].push_back(u);
      }
    }

    test = true;
    for(int u = 1; u <= m and test; u++){
      if(!color[u]) dfs(u, 1);
    }

    printf("%s\n", test ? "YES" : "NO");
  }
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn], g[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    if (d[v] < d[u])
    {
        d[u] = v;
        d[v] = s;
    }
    else
    {
        d[v] = u;
        d[u] = s;
    }
}

void dfs(int u, int c)
{
    g[u] = c;
    for(int v : ke[u])
    {
        if (g[v] == c) NO;
        if (g[v] == 0) dfs(v,3-c);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(i);
        }
    }
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1 && x != y) Union(x,y);
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    For(i,1,m)
    {
        if (g[i] == 0) dfs(i,1);
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    /*
    Forv(i,ke[v])
    {
        int x = ke[v][i];
        if (x == u) NO;
        ke[u].pb(x);
        ke[x].pb(u);
    }
    */
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(x);
            ke[x].pb(y);
        }
    }
    bool kt = false;
    For(i,1,m)
    {
        if (ke[i].size() == n) kt = true;
        ke[i].clear();
    }
    if (!kt) NO;
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1 && x == y) NO;
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn], g[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

void dfs(int u, int c)
{
    g[u] = c;
    for(int v : ke[u])
    {
        if (g[v] == c) NO;
        if (g[v] == 0) dfs(v,3-c);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(x);
            ke[x].pb(y);
        }
    }
    bool kt = false;
    For(i,1,m)
    {
        if (int(ke[i].size()) == n) kt = true;
        ke[i].clear();
    }
    //if (!kt) NO;
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1 && x == y) NO;
        ke[x].pb(y);
        ke[y].pb(x);
    }
    For(i,1,m)
    {
        if (g[i] == 0) dfs(i,1);
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn], g[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

void dfs(int u, int c)
{
    g[u] = c;
    for(int v : ke[u])
    {
        if (g[v] == c) NO;
        if (g[v] == 0) dfs(v,3-c);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(i);
        }
    }
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    For(i,1,m)
    {
        if (g[i] == 0) dfs(i,1);
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define mp make_pair
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int n, m, a[maxn], d[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    if (d[u] > d[v]) swap(u,v);
    d[v] = u;
    d[u] = s;
    vector <int> temp;
    Forv(ii,ke[v])
        {
            int vv = ke[v][ii];
            if (root(vv) == u)
            {
                cout << "NO";
                exit(0);
            }
            ke[u].pb(vv);
            //ke[vv].pb(u);
        }
    ke[v].clear();
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    For(i,1,n) cin >> a[i];
    bool kt = false;
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(i,1,x)
        {
            cin >> y;
            K[y].pb(x);
            ke[x].pb(y);
        }
        if (ke[x].size() == n) kt = true;
    }
    if (kt == false)
    {
        cout << "NO";
        return 0;
    }
    For(i,1,m) ke[i].clear();
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        if (a[i] == 0)
        {
            int x = root(u), y = root(v);
            if (x != y) Union(x,y);
        }
        else
        {
             int x = root(u), y = root(v);
             if (x == y)
             {
                 cout << "NO";
                 return 0;
             }
             ke[x].pb(y);
             ke[y].pb(x);
        }
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn], g[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

void dfs(int u, int c)
{
    g[u] = c;
    for(int v : ke[u])
    {
        if (g[v] == c) NO;
        if (g[v] == 0) dfs(v,3-c);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(i);
            ke[i].pb(y);
        }
    }
    bool kt = false;
    For(i,1,m)
    {
        if (int(ke[i].size()) == n) kt = true;
        ke[i].clear();
    }
    if (!kt) NO;
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1]; x
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1)
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    For(i,1,m)
    {
        if (g[i] == 0) dfs(i,1);
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    Forv(i,ke[v])
    {
        int x = ke[v][i];
        if (x == u) NO;
        ke[u].pb(x);
        ke[x].pb(u);
    }
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    For(i,1,n)
        cin >> a[i];
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(x);
        }
    }
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
        else
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    Forv(i,ke[v])
    {
        int x = ke[v][i];
        if (x == u) NO;
        ke[u].pb(x);
        ke[x].pb(u);
    }
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(x);
            ke[x].pb(y);
        }
    }
    bool kt = false;
    For(i,1,m)
    {
        if (ke[i].size() == n) kt = true;
        ke[i].clear();
    }
    if (!kt) NO;
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
        else
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn], g[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

void dfs(int u, int c)
{
    g[u] = c;
    for(int v : ke[u])
    {
        if (g[v] == c) NO;
        if (g[v] == 0) dfs(v,3-c);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(i);
            ke[i].pb(y);
        }
    }
    bool kt = false;
    For(i,1,m)
    {
        if (int(ke[i].size()) == n) kt = true;
        ke[i].clear();
    }
    if (!kt) NO;
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1)
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    For(i,1,m)
    {
        if (g[i] == 0) dfs(i,1);
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn], g[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

void dfs(int u, int c)
{
    g[u] = c;
    for(int v : ke[u])
    {
        if (g[v] == c) NO;
        if (g[v] == 0) dfs(v,3-c);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(x);
            ke[x].pb(y);
        }
    }
    bool kt = false;
    For(i,1,m)
    {
        if (int(ke[i].size()) == n) kt = true;
        ke[i].clear();
    }
    if (!kt) NO;
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1)
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    For(i,1,m)
    {
        if (g[i] == 0) dfs(i,1);
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn], g[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

void dfs(int u, int c)
{
    g[u] = c;
    for(int v : ke[u])
    {
        if (g[v] == c) NO;
        if (g[v] == 0) dfs(v,3-c);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(x);
            ke[x].pb(y);
        }
    }
    bool kt = false;
    For(i,1,m)
    {
        if (int(ke[i].size()) == n) kt = true;
        ke[i].clear();
    }
    //if (!kt) NO;
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1)
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    For(i,1,m)
    {
        if (g[i] == 0) dfs(i,1);
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn], g[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

void dfs(int u, int c)
{
    g[u] = c;
    for(int v : ke[u])
    {
        if (g[v] == c) NO;
        if (g[v] == 0) dfs(v,3-c);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(i);
        }
    }
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define mp make_pair
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int n, m, a[maxn], d[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    if (d[u] > d[v]) swap(u,v);
    d[v] = u;
    d[u] = s;
    vector <int> temp;
    Forv(ii,ke[v])
        {
            int vv = ke[v][ii];
            if (root(vv) == u)
            {
                cout << "NO";
                exit(0);
            }
            ke[u].pb(vv);
            //ke[vv].pb(u);
        }
    ke[v].clear();
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    For(i,1,n) cin >> a[i];
    bool kt = false;
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(i,1,x)
        {
            cin >> y;
            K[y].pb(x);
            ke[x].pb(y);
        }
        if (ke[x].size() == n) kt = true;
    }
    For(i,1,n) ke[i].clear();
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        if (a[i] == 0)
        {
            int x = root(u), y = root(v);
            if (x != y) Union(x,y);
        }
        else
        {
             int x = root(u), y = root(v);
             if (x == y)
             {
                 cout << "NO";
                 return 0;
             }
             ke[x].pb(y);
             ke[y].pb(x);
        }
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    Forv(i,ke[v])
    {
        int x = ke[v][i];
        if (x == u) NO;
        ke[u].pb(x);
        ke[x].pb(u);
    }
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(x);
        }
    }
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
        else
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn], g[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

void dfs(int u, int c)
{
    g[u] = c;
    for(int v : ke[u])
    {
        if (g[v] == c) NO;
        if (g[v] == 0) dfs(v,3-c);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(i);
            //ke[i].pb(y);
        }
    }
      For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1)
        {
            if (x == y) NO;
            ke[x].pb(y);
            ke[y].pb(x);
        }
    }
    For(i,1,m)
    {
        if (g[i] == 0) dfs(i,1);
    }
    cout << "YES";
}


//#ifndef ONLINE_JUDGE
#include <bits/stdc++.h>
#define maxn 100010
#define cmax 1000000007
#define llmax (1LL << 61)
#define el '\n'
#define pb push_back
#define NO {cout << "NO"; exit(0);}
#define mp make_pair
#define inmap(Map,x) ((Map).find((x)) != (Map).end())
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define Forv(i,v) for(int i = 0, _v = v.size(); i < _v; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> ptq;

int d[maxn], n, m, a[maxn];
vector <int> K[maxn], ke[maxn];

int root(int u)
{
    if (d[u] < 0) return u;
    return u = root(d[u]);
}

void Union(int u, int v)
{
    /*
    Forv(i,ke[v])
    {
        int x = ke[v][i];
        if (x == u) NO;
        ke[u].pb(x);
        ke[x].pb(u);
    }
    */
    int s = d[u] + d[v];
    d[v] = u;
    d[u] = s;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("a.txt","r"))
    {
        freopen("a.txt","r",stdin);
        //freopen("b.txt","w",stdout);
    }
    cin >> n >> m;
    int sum = 0;
    For(i,1,n)
        cin >> a[i], sum += a[i];
    if (sum == n)
    {
        cout << "YES";
        return 0;
    }
    For(i,1,m)
    {
        int x, y;
        cin >> x;
        For(j,1,x)
        {
            cin >> y;
            K[y].pb(x);
            ke[x].pb(y);
        }
    }
    bool kt = false;
    For(i,1,m)
    {
        if (ke[i].size() == n) kt = true;
        ke[i].clear();
    }
    //if (!kt) NO;
    For(i,1,m) d[i] = -1;
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 0)
        {
            if (x != y)
            {
                if (d[x] > d[y]) Union(x,y);
                else Union(y,x);
            }
        }
    }
    For(i,1,n)
    {
        int u = K[i][0], v = K[i][1];
        int x = root(u), y = root(v);
        if (a[i] == 1 && x == y) NO;
    }
    cout << "YES";
}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 1e5 + 7;
vector<int> adj[3*MAXN], radj[3*MAXN], room[3*MAXN];
stack<int> st;
bool vis[3*MAXN];
int n, m, status[3*MAXN], mark[3*MAXN];
int ct = 0;
void dfs1(int x){
	vis[x] = true;
	for(int i = 0;i < adj[x].size();i++){
		int u = adj[x][i];
		if(!vis[u])dfs1(u);
	}
	st.push(x);
}
void dfs2(int x){
	vis[x] = true;
	mark[x] = ct;
	//cout << x << " ";
	for(int i = 0;i < radj[x].size();i++){
		int u = radj[x][i];
		if(!vis[u])dfs2(u);
	}
}
int scc(){
	for(int i = 0;i <= 2*n+1;i++)vis[i] = 0;
	for(int i = 1;i <= 2*n;i++){
		if(!vis[i])dfs1(i);
	}
	for(int i = 0;i <= 2*n+1;i++)vis[i] = 0;
	while(!st.empty()){
		int u = st.top();
		st.pop();
		if(!vis[u]){
			ct++;
			dfs2(u);
			//cout << "\n";
		}
	}
}
void addEdge(int x,int y){
	adj[x].push_back(y);
	radj[y].push_back(x);
}
int main(){
	cin >> n >> m;
	for(int i = 1;i <= n;i++)cin >> status[i];
	for(int i = 1;i <= m;i++){
		int x;
		cin >> x;
		for(int j = 1;j <= x;j++){
			int y;
			cin >> y;
			room[y].push_back(i);
		}
	}
	for(int i = 1;i <= n;i++){
		int x = room[i][0];
		int y = room[i][1];
		if(status[i]==1){
			addEdge(x,y);
			addEdge(x+n,y+n);
			addEdge(y,x);
			addEdge(y+n,x+n);
		}
		else{
			addEdge(x+n,y);
			addEdge(x,y+n);
			addEdge(y+n,x);
			addEdge(y,x+n);
		}
	}
	scc();
	for(int i = 1;i <= n;i++){
		//cout << mark[i] << " " << mark[i+n] << endl;
		if(mark[i] == mark[i+n]){
			cout << "NO";
			return 0;
		}
	}
	cout << "YES";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 1e5 + 7;
vector<int> adj[MAXN], radj[MAXN], room[MAXN];
stack<int> st;
bool vis[MAXN];
int n, m, status[MAXN], mark[MAXN];
int ct = 0;
void dfs1(int x){
	vis[x] = true;
	for(int i = 0;i < adj[x].size();i++){
		int u = adj[x][i];
		if(!vis[u])dfs1(u);
	}
	st.push(x);
}
void dfs2(int x){
	vis[x] = true;
	mark[x] = ct;
	//cout << x << " ";
	for(int i = 0;i < radj[x].size();i++){
		int u = radj[x][i];
		if(!vis[u])dfs2(u);
	}
}
int scc(){
	for(int i = 0;i <= 2*n+1;i++)vis[i] = 0;
	for(int i = 1;i <= 2*n;i++){
		if(!vis[i])dfs1(i);
	}
	for(int i = 0;i <= 2*n+1;i++)vis[i] = 0;
	while(!st.empty()){
		int u = st.top();
		st.pop();
		if(!vis[u]){
			ct++;
			dfs2(u);
			//cout << "\n";
		}
	}
}
void addEdge(int x,int y){
	adj[x].push_back(y);
	radj[y].push_back(x);
}
int main(){
	cin >> n >> m;
	for(int i = 1;i <= n;i++)cin >> status[i];
	for(int i = 1;i <= m;i++){
		int x;
		cin >> x;
		for(int j = 1;j <= x;j++){
			int y;
			cin >> y;
			room[y].push_back(i);
		}
	}
	for(int i = 1;i <= n;i++){
		int x = room[i][0];
		int y = room[i][1];
		if(status[i]==1){
			addEdge(x,y);
			addEdge(x+n,y+n);
			addEdge(y,x);
			addEdge(y+n,x+n);
		}
		else{
			addEdge(x+n,y);
			addEdge(x,y+n);
			addEdge(y+n,x);
			addEdge(y,x+n);
		}
	}
	scc();
	for(int i = 1;i <= n;i++){
		//cout << mark[i] << " " << mark[i+n] << endl;
		if(mark[i] == mark[i+n]){
			cout << "NO";
			return 0;
		}
	}
	cout << "YES";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 1e5 + 7;
vector<int> adj[3*MAXN], radj[3*MAXN], room[3*MAXN];
stack<int> st;
bool vis[3*MAXN];
int n, m, status[3*MAXN], mark[3*MAXN];
int ct = 0;
void dfs1(int x){
	vis[x] = true;
	for(int i = 0;i < adj[x].size();i++){
		int u = adj[x][i];
		if(!vis[u])dfs1(u);
	}
	st.push(x);
}
void dfs2(int x){
	vis[x] = true;
	mark[x] = ct;
	for(int i = 0;i < radj[x].size();i++){
		int u = radj[x][i];
		if(!vis[u])dfs2(u);
	}
}
int scc(){
	for(int i = 0;i <= 2*m+1;i++)vis[i] = 0;
	for(int i = 1;i <= 2*m;i++){
		if(!vis[i])dfs1(i);
	}
	for(int i = 0;i <= 2*m+1;i++)vis[i] = 0;
	while(!st.empty()){
		int u = st.top();
		st.pop();
		if(!vis[u]){
			ct++;
			dfs2(u);
		}
	}
}
void addEdge(int x,int y){
	adj[x^1].push_back(y);
	adj[y^1].push_back(x);
	radj[x].push_back(y^1);
	radj[y].push_back(x^1);
}
int main(){
	cin >> n >> m;
	for(int i = 1;i <= n;i++)cin >> status[i];
	for(int i = 1;i <= m;i++){
		int x;
		cin >> x;
		for(int j = 1;j <= x;j++){
			int y;
			cin >> y;
			room[y].push_back(i);
		}
	}
	for(int i = 1;i <= n;i++){
		int x = room[i][0];
		int y = room[i][1];
		if(status[i]==1){
			addEdge(x,y^1);
			addEdge(x^1,y);
		}
		else{
			addEdge(x,y);
			addEdge(x^1,y^1);
		}
	}
	scc();
	for(int i = 1;i <= m;i++){
		if(mark[i] == mark[i^1]){
			cout << "NO";
			return 0;
		}
	}
	cout << "YES";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 1e5 + 7;
vector<int> adj[3*MAXN], radj[3*MAXN], room[3*MAXN];
stack<int> st;
bool vis[3*MAXN];
int n, m, status[3*MAXN], mark[3*MAXN];
int ct = 0;
void dfs1(int x){
	vis[x] = true;
	for(int i = 0;i < adj[x].size();i++){
		int u = adj[x][i];
		if(!vis[u])dfs1(u);
	}
	st.push(x);
}
void dfs2(int x){
	vis[x] = true;
	mark[x] = ct;
	//cout << x << " ";
	for(int i = 0;i < radj[x].size();i++){
		int u = radj[x][i];
		if(!vis[u])dfs2(u);
	}
}
int scc(){
	for(int i = 0;i <= 2*n+1;i++)vis[i] = 0;
	for(int i = 1;i <= 2*n;i++){
		if(!vis[i])dfs1(i);
	}
	for(int i = 0;i <= 2*n+1;i++)vis[i] = 0;
	while(!st.empty()){
		int u = st.top();
		st.pop();
		if(!vis[u]){
			ct++;
			dfs2(u);
			//cout << "\n";
		}
	}
}
void addEdge(int x,int y){
	adj[x].push_back(y);
	radj[y].push_back(x);
}
int main(){
	cin >> n >> m;
	for(int i = 1;i <= n;i++)cin >> status[i];
	for(int i = 1;i <= m;i++){
		int x;
		cin >> x;
		for(int j = 1;j <= x;j++){
			int y;
			cin >> y;
			room[y].push_back(i);
		}
	}
	for(int i = 1;i <= n;i++){
		int x = room[i][0];
		int y = room[i][1];
		if(status[i]==1){
			addEdge(x,y);
			addEdge(x+m,y+m);
			addEdge(y,x);
			addEdge(y+m,x+m);
		}
		else{
			addEdge(x+m,y);
			addEdge(x,y+m);
			addEdge(y+m,x);
			addEdge(y,x+m);
		}
	}
	scc();
	for(int i = 1;i <= m;i++){
		//cout << mark[i] << " " << mark[i+n] << endl;
		if(mark[i] == mark[i+m]){
			cout << "NO";
			return 0;
		}
	}
	cout << "YES";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ID(x) (x<<1)
const int MAXN = 1e5 + 7;
vector<int> adj[3*MAXN], radj[3*MAXN], room[3*MAXN];
stack<int> st;
bool vis[3*MAXN];
int n, m, status[3*MAXN], mark[3*MAXN];
int ct = 0;
void dfs1(int x){
	vis[x] = true;
	for(int i = 0;i < adj[x].size();i++){
		int u = adj[x][i];
		if(!vis[u])dfs1(u);
	}
	st.push(x);
}
void dfs2(int x){
	vis[x] = true;
	mark[x] = ct;
	for(int i = 0;i < radj[x].size();i++){
		int u = radj[x][i];
		if(!vis[u])dfs2(u);
	}
}
int scc(){
	for(int i = 0;i <= 2*m+1;i++)vis[i] = 0;
	for(int i = 1;i <= 2*m;i++){
		if(!vis[i])dfs1(i);
	}
	for(int i = 0;i <= 2*m+1;i++)vis[i] = 0;
	while(!st.empty()){
		int u = st.top();
		st.pop();
		if(!vis[u]){
			ct++;
			dfs2(u);
		}
	}
}
void addEdge(int x,int y){
	adj[x^1].push_back(y);
	adj[y^1].push_back(x);
	radj[x].push_back(y^1);
	radj[y].push_back(x^1);
}
int main(){
	cin >> n >> m;
	for(int i = 1;i <= n;i++)cin >> status[i];
	for(int i = 1;i <= m;i++){
		int x;
		cin >> x;
		for(int j = 1;j <= x;j++){
			int y;
			cin >> y;
			room[y].push_back(i);
		}
	}
	for(int i = 1;i <= n;i++){
		int x = room[i][0];
		int y = room[i][1];
		if(status[i]==1){
			addEdge(ID(x),ID(y)^1);
			addEdge(ID(x)^1,ID(y));
		}
		else{
			addEdge(ID(x),ID(y));
			addEdge(ID(x)^1,ID(y)^1);
		}
	}
	scc();
	for(int i = 1;i <= m;i++){
		if(mark[i] == mark[i^1]){
			cout << "NO";
			return 0;
		}
	}
	cout << "YES";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ID(x) (x<<1)
const int MAXN = 1e5 + 7;
vector<int> adj[3*MAXN], radj[3*MAXN], room[3*MAXN];
stack<int> st;
bool vis[3*MAXN];
int n, m, status[3*MAXN], mark[3*MAXN];
int ct = 0;
void dfs1(int x){
	vis[x] = true;
	for(int i = 0;i < adj[x].size();i++){
		int u = adj[x][i];
		if(!vis[u])dfs1(u);
	}
	st.push(x);
}
void dfs2(int x){
	vis[x] = true;
	mark[x] = ct;
	for(int i = 0;i < radj[x].size();i++){
		int u = radj[x][i];
		if(!vis[u])dfs2(u);
	}
}
int scc(){
	for(int i = 0;i <= 2*m+1;i++)vis[i] = 0;
	for(int i = 1;i <= 2*m;i++){
		if(!vis[i])dfs1(i);
	}
	for(int i = 0;i <= 2*m+1;i++)vis[i] = 0;
	while(!st.empty()){
		int u = st.top();
		st.pop();
		if(!vis[u]){
			ct++;
			dfs2(u);
		}
	}
}
void addEdge(int x,int y){
	adj[x^1].push_back(y);
	adj[y^1].push_back(x);
	radj[x].push_back(y^1);
	radj[y].push_back(x^1);
}
int main(){
	cin >> n >> m;
	for(int i = 1;i <= n;i++)cin >> status[i];
	for(int i = 1;i <= m;i++){
		int x;
		cin >> x;
		for(int j = 1;j <= x;j++){
			int y;
			cin >> y;
			room[y].push_back(i);
		}
	}
	for(int i = 1;i <= n;i++){
		int x = room[i][0];
		int y = room[i][1];
		if(status[i]==1){
			addEdge(ID(x),ID(y)^1);
			addEdge(ID(x)^1,ID(y));
		}
		else{
			addEdge(ID(x),ID(y));
			addEdge(ID(x)^1,ID(y)^1);
		}
	}
	scc();
	for(int i = 1;i <= 2*m;i++){
		if(mark[i] == mark[i^1]){
			cout << "NO";
			return 0;
		}
	}
	cout << "YES";
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

vector<vector<pair<int, int> > > g;
vector<bool> used;
vector<int> col;
bool f = false;

void dfs(int u, int cl) {
	used[u] = true;
	col[u] = cl;
	for (auto v : g[u]) {
		if (!used[v.first]) {
			dfs(v.first, (cl + v.second) % 2);
		}
		else {
			if (col[v.first] != (cl + v.second) % 2) {
				f = true;
			}
		}
	}
}

int main()
{
	int n, m;
	cin >> n >> m;
	vector<vector<int> > a(n);
	vector<int> d(n);
	for (int i = 0; i < n; i++) {
		cin >> d[i];
	}
	for (int i = 0; i < m; i++) {
		int x;
		cin >> x;
		for (int j = 0; j < x; j++) {
			int e;
			cin >> e;
			a[e - 1].push_back(i);
		}
	}
	g.resize(n);
	used.resize(n);
	col.resize(n, -1);
	for (int i = 0; i < n; i++) {
		if (a[i].size() != 2) {
			cout << "fhjk" << endl;
			return 0;
		}
		int v1 = a[i][0];
		int v2 = a[i][1];
		if (v1 >= n || v2 >= n) {
			cout << "fhjk" << endl;
			return 0;
		}
		g[v1].push_back({ v2, 1 - d[i] });
		g[v2].push_back({ v1, 1 - d[i] });
	}
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			dfs(i, 0);
		}
	}
	if (f) {
		cout << "NO" << endl;
	}
	else {
		cout << "YES" << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

vector<vector<pair<int, int> > > g;
vector<bool> used;
vector<int> col;
bool f = false;

void dfs(int u, int cl) {
	used[u] = true;
	col[u] = cl;
	for (auto v : g[u]) {
		if (!used[v.first]) {
			dfs(v.first, (cl + v.second) % 2);
		}
		else {
			if (col[v.first] != (cl + v.second) % 2) {
				f = true;
			}
		}
	}
}

int main()
{
	int n, m;
	cin >> n >> m;
	vector<vector<int> > a(n);
	vector<int> d(n);
	for (int i = 0; i < n; i++) {
		cin >> d[i];
	}
	for (int i = 0; i < m; i++) {
		int x;
		cin >> x;
		for (int j = 0; j < x; j++) {
			int e;
			cin >> e;
			a[e - 1].push_back(i);
		}
	}
	g.resize(m);
	used.resize(m);
	col.resize(m, -1);
	for (int i = 0; i < n; i++) {
		int v1 = a[i][0];
		int v2 = a[i][1];
		g[v1].push_back({ v2, 1 - d[i] });
		g[v2].push_back({ v1, 1 - d[i] });
	}
	for (int i = 0; i < m; i++) {
		if (!used[i]) {
			dfs(i, 0);
		}
	}
	if (f) {
		cout << "NO" << endl;
	}
	else {
		cout << "YES" << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

vector<vector<pair<int, bool> > > g;
vector<bool> used;
vector<int> col;
bool f = false;

void dfs(int u, bool cl) {
	used[u] = true;
	col[u] = cl;
	for (auto v : g[u]) {
		if (!used[v.first]) {
			dfs(v.first, cl ^ v.second);
		}
		else {
			if (col[v.first] != cl ^ v.second) {
				f = true;
			}
		}
	}
}

int main()
{
	int n, m;
	cin >> n >> m;
	vector<vector<int> > a(n);
	vector<bool> d(n);
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		d[i] = x;
	}
	for (int i = 0; i < m; i++) {
		int x;
		cin >> x;
		for (int j = 0; j < x; j++) {
			int e;
			cin >> e;
			a[e - 1].push_back(i);
		}
	}
	g.resize(n);
	used.resize(n);
	col.resize(n, -1);
	for (int i = 0; i < n; i++) {
		int v1 = a[i][0];
		int v2 = a[i][1];
		g[v1].push_back({ v2, !d[i] });
		g[v2].push_back({ v1, !d[i] });
	}
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			dfs(i, 0);
		}
	}
	if (f) {
		cout << "NO" << endl;
	}
	else {
		cout << "YES" << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

vector<vector<pair<int, int> > > g;
vector<bool> used;
vector<int> col;
bool f = false;

void dfs(int u, int cl) {
	used[u] = true;
	col[u] = cl;
	for (auto v : g[u]) {
		if (!used[v.first]) {
			dfs(v.first, (cl + v.second) % 2);
		}
		else {
			if (col[v.first] != (cl + v.second) % 2) {
				f = true;
			}
		}
	}
}

int main()
{
	int n, m;
	cin >> n >> m;
	vector<vector<int> > a(n);
	vector<int> d(n);
	for (int i = 0; i < n; i++) {
		cin >> d[i];
	}
	for (int i = 0; i < m; i++) {
		int x;
		cin >> x;
		for (int j = 0; j < x; j++) {
			int e;
			cin >> e;
			a[e - 1].push_back(i);
		}
	}
	g.resize(n);
	used.resize(n);
	col.resize(n, -1);
	for (int i = 0; i < n; i++) {
		int v1 = a[i][0];
		int v2 = a[i][1];
		g[v1].push_back({ v2, 1 - d[i] });
		g[v2].push_back({ v1, 1 - d[i] });
	}
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			dfs(i, 0);
		}
	}
	if (f) {
		cout << "NO" << endl;
	}
	else {
		cout << "YES" << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

vector<vector<pair<int, int> > > g;
vector<bool> used;
vector<int> col;
bool f = false;

void dfs(int u, int cl) {
	used[u] = true;
	col[u] = cl;
	for (auto v : g[u]) {
		if (!used[v.first]) {
			dfs(v.first, (cl + v.second) % 2);
		}
		else {
			if (col[v.first] != (cl + v.second) % 2) {
				f = true;
			}
		}
	}
}

int main()
{
	int n, m;
	cin >> n >> m;
	vector<vector<int> > a(n);
	vector<int> d(n);
	for (int i = 0; i < n; i++) {
		cin >> d[i];
	}
	for (int i = 0; i < m; i++) {
		int x;
		cin >> x;
		for (int j = 0; j < x; j++) {
			int e;
			cin >> e;
			a[e - 1].push_back(i);
		}
	}
	g.resize(n);
	used.resize(n);
	col.resize(n, -1);
	for (int i = 0; i < n; i++) {
		if (a[i].size() != 2) {
			cout << "fhjk" << endl;
			return 0;
		}
		int v1 = a[i][0];
		int v2 = a[i][1];
		g[v1].push_back({ v2, 1 - d[i] });
		g[v2].push_back({ v1, 1 - d[i] });
	}
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			dfs(i, 0);
		}
	}
	if (f) {
		cout << "NO" << endl;
	}
	else {
		cout << "YES" << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN], cnt[MAXN], dr[MAXN][2], mrk[MAXN], mrk2[MAXN];

vector<int> adj[MAXN];
int tempo;

inline int no(int x){
	return x+m;
}

void go(int u){
	mrk[u] = tempo;
	for(int nxt: adj[u]){
		if(mrk[nxt]) continue;
		go(nxt);
	}
}

int main (){
	tempo = 1;
	scanf("%d%d", &n, &m);
	for(int a=0;a<n;a++){
		scanf("%d", &s[a]);
	}
	for(int a=0;a<m;a++){
		int t;
		scanf("%d", &t);
		while(t--){
			int x;
			scanf("%d", &x);
			x--;
			dr[x][cnt[x]++] = a;
		}
	}
	for(int a=0;a<n;a++){
		if(s[a]){
			adj[dr[a][0]].pb(dr[a][1]);
			adj[dr[a][1]].pb(dr[a][0]);
			adj[no(dr[a][0])].pb(no(dr[a][1]));
			adj[no(dr[a][1])].pb(no(dr[a][0]));
		}
		else{
			adj[dr[a][0]].pb(no(dr[a][1]));
			adj[dr[a][1]].pb(no(dr[a][0]));
			adj[no(dr[a][0])].pb(dr[a][1]);
			adj[no(dr[a][1])].pb(dr[a][0]);
		}
	}
	for(int a=0;a<no(m-1);a++){
		if(mrk[a]) continue;
		tempo++;
		go(a);
	}
	for(int a=0;a<m;a++){
		if(mrk[a] == mrk[no(a)]){
			puts("NO");
			return 0;
		}
	}
	puts("YES");
}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN], cnt[MAXN], dr[MAXN][2], mrk[MAXN], mrk2[MAXN];

vector<pii> adj[MAXN];
int fodeu;

void go(int i, int c){
	mrk2[i] = c;
	if(fodeu) return;
	for(pii nxt: adj[i]){
		if(fodeu) return;
		int v = nxt.fst;
		if(mrk2[v] != 0){
			if(nxt.snd == ((c-1)^(mrk2[v]-1))){
				fodeu = 1;
				return;
			}
			continue;
		}
		go(v, !((c-1)^nxt.snd) + 1);
	}
}

void limpa(int i){
	mrk2[i] = 0;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk2[v] != 0)
			limpa(v);
	}
}

void marca(int i, int c){
	mrk[i] = c;
	if(fodeu) return;
	for(pii nxt: adj[i]){
		if(fodeu) return;
		int v = nxt.fst;
		if(mrk[v] != 0){
			if(nxt.snd == ((c-1)^(mrk[v]-1))){
				fodeu = 1;
				return;
			}
			continue;
		}
		go(v, !((c-1)^nxt.snd) + 1);
	}
}

int main (){
	tempo = 1;
	scanf("%d%d", &n, &m);
	for(int a=0;a<n;a++){
		scanf("%d", &s[a]);
	}
	for(int a=0;a<m;a++){
		int t;
		scanf("%d", &t);
		while(t--){
			int x;
			scanf("%d", &x);
			x--;
			dr[x][cnt[x]++] = a;
		}
	}
	for(int a=0;a<n;a++){
		adj[dr[a][0]].pb(pii(dr[a][1], s[a]));
		adj[dr[a][1]].pb(pii(dr[a][0], s[a]));
	}
	for(int a=0;a<m;a++){
		if(mrk[a] != 0) continue;
		fodeu = 0;
		go(a, 2);
		if(!fodeu){
			marca(a, 2);
			continue;
		}
		limpa(a);
		fodeu = 0;
		go(a, 1);
		if(!fodeu){
			marca(a, 1);
			continue;
		}
		puts("NO");
		return 0;
	}
	puts("YES");
}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN], cnt[MAXN], dr[MAXN][2], mrk[MAXN], mrk2[MAXN];

vector<int> lv[MAXN];
vector<pii> adj[MAXN];
int fodeu;
int tempo;

void go(int i, int c){
	mrk2[i] = c;
	if(fodeu) return;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk2[v] != 0){
			if(nxt.snd == ((c-1)^(mrk2[v]-1))){
				fodeu = 1;
				return;
			}
			continue;
		}
		go(v, !((c-1)^nxt.snd) + 1);
	}
}

void limpa(int i){
	mrk2[i] = 0;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk2[v] != 0)
			limpa(v);
	}
}

void marca(int i, int c){
	mrk[i] = c;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk[v] != 0){
			if(nxt.snd == ((c-1)^(mrk[v]-1))){
				fodeu = 1;
				return;
			}
			continue;
		}
		go(v, !((c-1)^nxt.snd) + 1);
	}
}

int main (){
	tempo = 1;
	scanf("%d%d", &n, &m);
	for(int a=0;a<n;a++){
		scanf("%d", &s[a]);
	}
	for(int a=0;a<m;a++){
		int t;
		scanf("%d", &t);
		while(t--){
			int x;
			scanf("%d", &x);
			x--;
			lv[a].pb(x);
			dr[x][cnt[x]++] = a;
		}
	}
	for(int a=0;a<n;a++){
		adj[dr[a][0]].pb(pii(dr[a][1], s[a]));
		adj[dr[a][1]].pb(pii(dr[a][0], s[a]));
	}
	for(int a=0;a<m;a++){
		if(mrk[a]) continue;
		fodeu = 0;
		go(a, 2);
		if(!fodeu){
			marca(a, 2);
			continue;
		}
		limpa(a);
		fodeu = 0;
		go(a, 1);
		if(!fodeu){
			marca(a, 1);
			continue;
		}
		puts("NO");
		return 0;
	}
	puts("YES");
}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN], cnt[MAXN], dr[MAXN][2], mrk[MAXN], mrk2[MAXN];

vector<int> lv[MAXN];
vector<pii> adj[MAXN];
int fodeu;
int tempo;

void go(int i, int c){
	mrk2[i] = c;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk2[v] != 0){
			if(nxt.snd == ((c-1)^(mrk2[v]-1))){
				fodeu = 1;
				return;
			}
			continue;
		}
		go(v, !((c-1)^nxt.snd) + 1);
	}
}

void limpa(int i){
	mrk2[i] = 0;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk2[v] != 0)
			limpa(v);
	}
}

void marca(int i, int c){
	mrk[i] = c;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk[v] != 0){
			if(nxt.snd == ((c-1)^(mrk[v]-1))){
				fodeu = 1;
				return;
			}
			continue;
		}
		go(v, !((c-1)^nxt.snd) + 1);
	}
}

int main (){
	tempo = 1;
	scanf("%d%d", &n, &m);
	for(int a=0;a<n;a++){
		scanf("%d", &s[a]);
	}
	for(int a=0;a<m;a++){
		int t;
		scanf("%d", &t);
		while(t--){
			int x;
			scanf("%d", &x);
			x--;
			lv[a].pb(x);
			dr[x][cnt[x]++] = a;
		}
	}
	for(int a=0;a<n;a++){
		adj[dr[a][0]].pb(pii(dr[a][1], s[a]));
		adj[dr[a][1]].pb(pii(dr[a][0], s[a]));
	}
	for(int a=0;a<m;a++){
		if(mrk[a]) continue;
		fodeu = 0;
		go(a, 0);
		if(!fodeu){
			marca(a, 2);
			continue;
		}
		limpa(a);
		fodeu = 0;
		go(a, 1);
		if(!fodeu){
			marca(a, 1);
			continue;
		}
		puts("NO");
		return 0;
	}
	puts("YES");
}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN], cnt[MAXN], dr[MAXN][2], mrk[MAXN], mrk2[MAXN];

vector<int> lv[MAXN];
vector<pii> adj[MAXN];
int fodeu;
int tempo;

void go(int i, int c){
	mrk2[i] = c;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk2[v] != 0){
			if(nxt.snd == ((c-1)^(mrk2[v]-1))){
				fodeu = 1;
				return;
			}
			continue;
		}
		go(v, !((c-1)^nxt.snd) + 1);
	}
}

void limpa(int i){
	mrk2[i] = 0;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk2[v] != 0)
			limpa(v);
	}
}

void marca(int i, int c){
	mrk[i] = c;
	for(pii nxt: adj[i]){
		int v = nxt.fst;
		if(mrk[v] != 0){
			if(nxt.snd == ((c-1)^(mrk[v]-1))){
				fodeu = 1;
				return;
			}
			continue;
		}
		go(v, !((c-1)^nxt.snd) + 1);
	}
}

int main (){
	tempo = 1;
	scanf("%d%d", &n, &m);
	for(int a=0;a<n;a++){
		scanf("%d", &s[a]);
	}
	for(int a=0;a<m;a++){
		int t;
		scanf("%d", &t);
		while(t--){
			int x;
			scanf("%d", &x);
			x--;
			lv[a].pb(x);
			dr[x][cnt[x]++] = a;
		}
	}
	for(int a=0;a<n;a++){
		adj[dr[a][0]].pb(pii(dr[a][1], s[a]));
		adj[dr[a][1]].pb(pii(dr[a][0], s[a]));
	}
	for(int a=0;a<m;a++){
		if(mrk[a]) continue;
		fodeu = 0;
		go(a, 2);
		if(!fodeu){
			marca(a, 2);
			continue;
		}
		limpa(a);
		fodeu = 0;
		go(a, 1);
		if(!fodeu){
			marca(a, 1);
			continue;
		}
		puts("NO");
		return 0;
	}
	puts("YES");
}


// written at 22:17 on 23 Feb 2017
#include <bits/stdc++.h>

#define IOS std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
// #define __DEBUG__
#ifdef __DEBUG__
	#define DEBUG(...) printf(__VA_ARGS__)
#else
	#define DEBUG(...)
#endif
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".ans", "w", stdout);
#define resetfile() freopen("/dev/tty", "r", stdin); freopen("/dev/tty", "w", stdout); system("more " filename".ans");
#define rep(i, j, k) for (int i = j; i < k; ++i)
#define irep(i, j, k) for (int i = j - 1; i >= k; --i)

using namespace std;

template <typename T>
inline T sqr(T a) { return a * a;};

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int > Pii;

const double pi = acos(-1.0);
const int INF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = 1e5 + 10;

int N, M, r[MAX_N], x, xi;
vector<int> s[MAX_N];

int par[MAX_N * 2];
int rnk[MAX_N * 2];
void init(int n) {
    for (int i = 0; i < n; ++i) {
        par[i] = i;
        rnk[i] = 0;
    }
}
int find(int x) {
    return par[x] == x? x : par[x] = find(par[x]);
}
bool same(int x, int y) {
    return find(x) == find(y);
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rnk[x] < rnk[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
}

int main(int argc, char const *argv[])
{
	scanf("%d%d", &N, &M);
	init(2 * M);
	rep(i, 0, N) scanf("%d", r + i);
	rep(i, 0, M) {
		scanf("%d", &x);
		rep(j, 0, x) {
			scanf("%d", &xi), --xi;
			s[xi].push_back(i);
		}
	}
	rep(i, 0, N) {
		int u = s[i][0], v = s[i][1];
		if (r[i]) unite(u, v), unite(u + M, v + M);
		else unite(u, v + M), unite(u, v + M);
	}
	bool flag = true;
	rep(i, 0, N) {
		int u = s[i][0], v = s[i][1];
		if (r[i]) {
			if (same(u, v + M)) { flag = false; break;}
		} else {
			if (same(u, v)) { flag = false; break;}
		}
	}
	puts(flag? "YES" : "NO");
	return 0;
}


// written at 22:17 on 23 Feb 2017
#include <bits/stdc++.h>

#define IOS std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
// #define __DEBUG__
#ifdef __DEBUG__
	#define DEBUG(...) printf(__VA_ARGS__)
#else
	#define DEBUG(...)
#endif
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".ans", "w", stdout);
#define resetfile() freopen("/dev/tty", "r", stdin); freopen("/dev/tty", "w", stdout); system("more " filename".ans");
#define rep(i, j, k) for (int i = j; i < k; ++i)
#define irep(i, j, k) for (int i = j - 1; i >= k; --i)

using namespace std;

template <typename T>
inline T sqr(T a) { return a * a;};

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int > Pii;

const double pi = acos(-1.0);
const int INF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = 1e5 + 10;

int N, M, r[MAX_N], x, xi;
vector<int> s[MAX_N];

int par[MAX_N];
int rnk[MAX_N];
void init(int n) {
    for (int i = 0; i < n; ++i) {
        par[i] = i;
        rnk[i] = 0;
    }
}
int find(int x) {
    return par[x] == x? x : par[x] = find(par[x]);
}
bool same(int x, int y) {
    return find(x) == find(y);
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rnk[x] < rnk[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
}

int main(int argc, char const *argv[])
{
	scanf("%d%d", &N, &M);
	init(M);
	rep(i, 0, N) scanf("%d", r + i);
	rep(i, 0, M) {
		scanf("%d", &x);
		rep(j, 0, x) {
			scanf("%d", &xi); --xi;
			s[xi].push_back(i);
		}
	}
	rep(i, 0, N) if (r[i]) {
		int u = s[i][0], v = s[i][1];
		// cout << u << " " << v << endl;
		unite(u, v);
	}
	bool flag = true;
	rep(i, 0, N) if (!r[i]) {
		int u = s[i][0], v = s[i][1];
		if (same(u, v)) {flag = false; break;}
	}
	if (flag) printf("YES\n"); else printf("NO\n");
	return 0;
}


// written at 22:17 on 23 Feb 2017
#include <bits/stdc++.h>

#define IOS std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
// #define __DEBUG__
#ifdef __DEBUG__
	#define DEBUG(...) printf(__VA_ARGS__)
#else
	#define DEBUG(...)
#endif
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".ans", "w", stdout);
#define resetfile() freopen("/dev/tty", "r", stdin); freopen("/dev/tty", "w", stdout); system("more " filename".ans");
#define rep(i, j, k) for (int i = j; i < k; ++i)
#define irep(i, j, k) for (int i = j - 1; i >= k; --i)

using namespace std;

template <typename T>
inline T sqr(T a) { return a * a;};

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int > Pii;

const double pi = acos(-1.0);
const int INF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = 1e6 + 10;

int N, M, r[MAX_N], x, xi;
vector<int> s[MAX_N];

int par[MAX_N * 2];
int rnk[MAX_N * 2];
void init(int n) {
    for (int i = 0; i < n; ++i) {
        par[i] = i;
        rnk[i] = 0;
    }
}
int find(int x) {
    return par[x] == x? x : par[x] = find(par[x]);
}
bool same(int x, int y) {
    return find(x) == find(y);
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rnk[x] < rnk[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
}

int main(int argc, char const *argv[])
{
	scanf("%d%d", &N, &M);
	init(2 * M);
	rep(i, 0, N) scanf("%d", r + i);
	rep(i, 0, M) {
		scanf("%d", &x);
		rep(j, 0, x) {
			scanf("%d", &xi), --xi;
			s[xi].push_back(i);
		}
	}
	rep(i, 0, N) {
		int u = s[i][0], v = s[i][1];
		if (r[i]) unite(u, v), unite(u + M, v + M);
		else unite(u, v + M), unite(u + M, v);
	}
	bool flag = true;
	rep(i, 0, M) if (same(i, i + M)) { flag = false;break;}
	puts(flag? "YES" : "NO");
	return 0;
}


// written at 22:17 on 23 Feb 2017
#include <bits/stdc++.h>

#define IOS std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
// #define __DEBUG__
#ifdef __DEBUG__
	#define DEBUG(...) printf(__VA_ARGS__)
#else
	#define DEBUG(...)
#endif
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".ans", "w", stdout);
#define resetfile() freopen("/dev/tty", "r", stdin); freopen("/dev/tty", "w", stdout); system("more " filename".ans");
#define rep(i, j, k) for (int i = j; i < k; ++i)
#define irep(i, j, k) for (int i = j - 1; i >= k; --i)

using namespace std;

template <typename T>
inline T sqr(T a) { return a * a;};

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int > Pii;

const double pi = acos(-1.0);
const int INF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = 1e6 + 10;

int N, M, r[MAX_N], x, xi;
vector<int> s[MAX_N];

int par[MAX_N * 2];
int rnk[MAX_N * 2];
void init(int n) {
    for (int i = 0; i < n; ++i) {
        par[i] = i;
        rnk[i] = 0;
    }
}
int find(int x) {
    return par[x] == x? x : par[x] = find(par[x]);
}
bool same(int x, int y) {
    return find(x) == find(y);
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rnk[x] < rnk[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
}

int main(int argc, char const *argv[])
{
	scanf("%d%d", &N, &M);
	init(2 * M);
	rep(i, 0, N) scanf("%d", r + i);
	rep(i, 0, M) {
		scanf("%d", &x);
		rep(j, 0, x) {
			scanf("%d", &xi), --xi;
			s[xi].push_back(i);
		}
	}
	rep(i, 0, N) {
		int u = s[i][0], v = s[i][1];
		if (r[i]) unite(u, v), unite(u + M, v + M);
		else unite(u, v + M), unite(u, v + M);
	}
	bool flag = true;
	rep(i, 0, M) if (same(i, i + M)) { flag = false;break;}
	puts(flag? "YES" : "NO");
	return 0;
}


// written at 22:17 on 23 Feb 2017
#include <bits/stdc++.h>

#define IOS std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
// #define __DEBUG__
#ifdef __DEBUG__
	#define DEBUG(...) printf(__VA_ARGS__)
#else
	#define DEBUG(...)
#endif
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".ans", "w", stdout);
#define resetfile() freopen("/dev/tty", "r", stdin); freopen("/dev/tty", "w", stdout); system("more " filename".ans");
#define rep(i, j, k) for (int i = j; i < k; ++i)
#define irep(i, j, k) for (int i = j - 1; i >= k; --i)

using namespace std;

template <typename T>
inline T sqr(T a) { return a * a;};

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int > Pii;

const double pi = acos(-1.0);
const int INF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = 1e5 + 10;

int N, M, r[MAX_N], x, xi;
vector<int> s[MAX_N];

int par[MAX_N];
int rnk[MAX_N];
void init(int n) {
    for (int i = 0; i < n; ++i) {
        par[i] = i;
        rnk[i] = 0;
    }
}
int find(int x) {
    return par[x] == x? x : par[x] = find(par[x]);
}
bool same(int x, int y) {
    return find(x) == find(y);
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rnk[x] < rnk[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
}

int main(int argc, char const *argv[])
{
	scanf("%d%d", &N, &M);
	init(2 * M);
	rep(i, 0, N) scanf("%d", r + i);
	rep(i, 0, M) {
		scanf("%d", &x);
		rep(j, 0, x) {
			scanf("%d", &xi), --xi;
			s[xi].push_back(i);
		}
	}
	rep(i, 0, N) {
		int u = s[i][0], v = s[i][1];
		if (r[i]) unite(u, v), unite(u + M, v + M);
		else unite(u, v + M), unite(u, v + M);
	}
	bool flag = true;
	rep(i, 0, N) {
		int u = s[i][0], v = s[i][1];
		if (r[i]) {
			if (same(u, v + M)) { flag = false; break;}
		} else {
			if (same(u, v)) { flag = false; break;}
		}
	}
	puts(flag? "YES" : "NO");
	return 0;
}


// written at 22:17 on 23 Feb 2017
#include <bits/stdc++.h>

#define IOS std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
// #define __DEBUG__
#ifdef __DEBUG__
	#define DEBUG(...) printf(__VA_ARGS__)
#else
	#define DEBUG(...)
#endif
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".ans", "w", stdout);
#define resetfile() freopen("/dev/tty", "r", stdin); freopen("/dev/tty", "w", stdout); system("more " filename".ans");
#define rep(i, j, k) for (int i = j; i < k; ++i)
#define irep(i, j, k) for (int i = j - 1; i >= k; --i)

using namespace std;

template <typename T>
inline T sqr(T a) { return a * a;};

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int > Pii;

const double pi = acos(-1.0);
const int INF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = 1e5 + 10;

int N, M, r[MAX_N], x, xi;
vector<int> s[MAX_N];

int par[MAX_N * 2];
int rnk[MAX_N * 2];
void init(int n) {
    for (int i = 0; i < n; ++i) {
        par[i] = i;
        rnk[i] = 0;
    }
}
int find(int x) {
    return par[x] == x? x : par[x] = find(par[x]);
}
bool same(int x, int y) {
    return find(x) == find(y);
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rnk[x] < rnk[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
}

int main(int argc, char const *argv[])
{
	scanf("%d%d", &N, &M);
	init(2 * M);
	rep(i, 0, N) scanf("%d", r + i);
	rep(i, 0, M) {
		scanf("%d", &x);
		rep(j, 0, x) {
			scanf("%d", &xi), --xi;
			s[xi].push_back(i);
		}
	}
	rep(i, 0, N) {
		int u = s[i][0], v = s[i][1];
		if (r[i]) unite(u, v), unite(u + M, v + M);
		else unite(u, v + M), unite(u, v + M);
	}
	bool flag = true;
	rep(i, 0, M) if (same(i, i + M)) { flag = false;break;}
	puts(flag? "YES" : "NO");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int r[maxn], low[maxn], dfn[maxn], vis[maxn], q[maxn], now, num1, num2, Scc[maxn];
vector<int>  s1[maxn], s2[maxn], s3[maxn];
map<pair<int, int>, int> pq;
int must[maxn];
void scc(int x){
    low[x] = dfn[x] = ++now;
    vis[x] = 1;
    q[num1++] = x;
    for(int i = 0; i < s2[x].size(); i++){
        int u = s2[x][i];
        if(low[u] == -1){
            scc(u);
            low[x] = min(low[x], low[u]);
        }
        else if(vis[u] == 1){
            low[x] = min(low[x], dfn[u]);
        }
    }
    if(low[x] == dfn[x]){
        int m;
        do{
            m = q[num1 - 1];
            Scc[m] = num2;
            num1--;
            vis[m] = 0;
        }while(m != x);
        num2++;
    }
}
int main() {
    now = num1 = num2 = 0;
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &r[i]);
    for(int i = 1; i <= m; i++){
        scanf("%d", &x);
        for(int j = 0; j < x; j++){
            scanf("%d", &y);
            s1[y].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        if(r[i] == 1){
            s2[u].push_back(v);
            s2[v].push_back(u);
        }
        else{
            must[u] = must[v] = 1;
            s3[u].push_back(v);
            s3[v].push_back(u);
        }
    }
    for(int i = 1; i <= n; i++){
        if(r[i] == 0){
            int u = s1[i][0];
            int v = s1[i][1];
            int t = min(u, v);
            u = max(u, v);
            v = t;
            if(pq[pair<int, int>(u, v)] == 1)
                continue;
            pq[pair<int, int>(u, v)] = 1;
            for(int j = 0; j < s3[u].size(); j++){
                int k = s3[u][j];
                if(must[k] == 1 && k != v)
                    s2[v].push_back(k);
            }
            for(int j = 0; j < s3[v].size(); j++){
                int k = s3[v][j];
                if(must[k] == 1 && k != u)
                    s2[u].push_back(k);
            }
        }
    }
    memset(low, -1, sizeof(low));
    for(int i = 1; i <= m; i++){
        if(low[i] == -1)
            scc(i);
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        if((Scc[u] == Scc[v] && r[i] == 0) || (Scc[u] != Scc[v] && r[i] == 1)){
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int r[maxn], low[maxn], dfn[maxn], vis[maxn], q[maxn], now, num1, num2, Scc[maxn];
vector<int>  s1[maxn], s2[maxn], s3[maxn];
map<pair<int, int>, int> pq;
int must[maxn];
void scc(int x){
    low[x] = dfn[x] = ++now;
    vis[x] = 1;
    q[num1++] = x;
    for(int i = 0; i < s2[x].size(); i++){
        int u = s2[x][i];
        if(low[u] == -1){
            scc(u);
            low[x] = min(low[x], low[u]);
        }
        else if(vis[u] == 1){
            low[x] = min(low[x], dfn[u]);
        }
    }
    if(low[x] == dfn[x]){
        int m;
        do{
            m = q[num1 - 1];
            Scc[m] = num2;
            num1--;
            vis[m] = 0;
        }while(m != x);
        num2++;
    }
}
int main() {
    now = num1 = num2 = 0;
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &r[i]);
    for(int i = 1; i <= m; i++){
        scanf("%d", &x);
        for(int j = 0; j < x; j++){
            scanf("%d", &y);
            s1[y].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        if(r[i] == 1){
            s2[u].push_back(v);
            s2[v].push_back(u);
        }
        else{
            must[u] = must[v] = 1;
            s3[u].push_back(v);
            s3[v].push_back(u);
        }
    }
    for(int i = 1; i <= n; i++){
        if(r[i] == 0){
            int u = s1[i][0];
            int v = s1[i][1];
            for(int j = 0; j < s3[u].size(); j++){
                int k = s3[u][j];
                if(must[k] == 1 && k != v)
                    s2[v].push_back(k);
            }
            for(int j = 0; j < s3[v].size(); j++){
                int k = s3[v][j];
                if(must[k] == 1 && k != u)
                    s2[u].push_back(k);
            }
        }
    }
    memset(low, -1, sizeof(low));
    for(int i = 1; i <= m; i++){
        if(low[i] == -1)
            scc(i);
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        if((Scc[u] == Scc[v] && r[i] == 0)){
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int r[maxn], low[maxn], dfn[maxn], vis[maxn], q[maxn], now, num1, num2, Scc[maxn];
vector<int>  s1[maxn], s2[maxn], s3[maxn];
map<pair<int, int>, int> pq1, pq2, pq3;
int must[maxn];
void scc(int x){
    low[x] = dfn[x] = ++now;
    vis[x] = 1;
    q[num1++] = x;
    for(int i = 0; i < s2[x].size(); i++){
        int u = s2[x][i];
        if(low[u] == -1){
            scc(u);
            low[x] = min(low[x], low[u]);
        }
        else if(vis[u] == 1){
            low[x] = min(low[x], dfn[u]);
        }
    }
    if(low[x] == dfn[x]){
        int m;
        do{
            m = q[num1 - 1];
            Scc[m] = num2;
            num1--;
            vis[m] = 0;
        }while(m != x);
        num2++;
    }
}
int main() {
    now = num1 = num2 = 0;
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &r[i]);
    for(int i = 1; i <= m; i++){
        scanf("%d", &x);
        for(int j = 0; j < x; j++){
            scanf("%d", &y);
            s1[y].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        int t = min(u, v);
        u = max(u, v);
        v = t;
        if(r[i] == 1){
            if(pq1[pair<int, int>(u, v)] == 0){
                pq1[pair<int, int>(u, v)] = 1;
                s2[u].push_back(v);
                s2[v].push_back(u);
            }
        }
        else{
            if(pq2[pair<int, int>(u, v)] == 0){
                pq2[pair<int, int>(u, v)] = 1;
                s3[u].push_back(v);
                s3[v].push_back(u);
            }
        }
    }
    for(int i = 1; i <= n; i++){
        if(r[i] == 0){
            int u = s1[i][0];
            int v = s1[i][1];
            int t = min(u, v);
            u = max(u, v);
            v = t;
            if(pq3[pair<int, int>(u, v)] == 1)
                continue;
            pq3[pair<int, int>(u, v)] = 1;
            for(int j = 0; j < s3[u].size(); j++){
                int k = s3[u][j];
                if(k != v){
                    t = min(v, k);
                    k = max(v, k);
                    v = t;
                    if(pq1[pair<int, int>(k, v)] == 0){
                        pq1[pair<int, int>(k, v)] = 1;
                        s2[v].push_back(k);
                        s2[k].push_back(v);
                    }
                }
            }
            for(int j = 0; j < s3[v].size(); j++){
                int k = s3[v][j];
                if(k != u){
                    t = min(u, k);
                    k = max(u, k);
                    u = t;
                    if(pq1[pair<int, int>(k, u)] == 0){
                        pq1[pair<int, int>(k, u)] = 1;
                        s2[u].push_back(k);
                        s2[k].push_back(u);
                    }
                }
            }
        }
    }
    memset(low, -1, sizeof(low));
    for(int i = 1; i <= m; i++){
        if(low[i] == -1)
            scc(i);
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        if((Scc[u] == Scc[v] && r[i] == 0) || (Scc[u] != Scc[v] && r[i] == 1)){
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int r[maxn], low[maxn], dfn[maxn], vis[maxn], q[maxn], now, num1, num2, Scc[maxn];
vector<int>  s1[maxn], s2[maxn], s3[maxn];
map<pair<int, int>, int> pq;
int must[maxn];
void scc(int x){
    low[x] = dfn[x] = ++now;
    vis[x] = 1;
    q[num1++] = x;
    for(int i = 0; i < s2[x].size(); i++){
        int u = s2[x][i];
        if(low[u] == -1){
            scc(u);
            low[x] = min(low[x], low[u]);
        }
        else if(vis[u] == 1){
            low[x] = min(low[x], dfn[u]);
        }
    }
    if(low[x] == dfn[x]){
        int m;
        do{
            m = q[num1 - 1];
            Scc[m] = num2;
            num1--;
            vis[m] = 0;
        }while(m != x);
        num2++;
    }
}
int main() {
    now = num1 = num2 = 0;
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &r[i]);
    for(int i = 1; i <= m; i++){
        scanf("%d", &x);
        for(int j = 0; j < x; j++){
            scanf("%d", &y);
            s1[y].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        if(r[i] == 1){
            s2[u].push_back(v);
            s2[v].push_back(u);
//            printf("%d @@@ %d\n", u, v);
        }
        else{
            must[u] = must[v] = 1;
            s3[u].push_back(v);
            s3[v].push_back(u);
        }
    }
    for(int i = 1; i <= n; i++){
        if(r[i] == 0){
            int u = s1[i][0];
            int v = s1[i][1];
            int t = min(u, v);
            u = max(u, v);
            v = t;
            if(pq[pair<int, int>(u, v)] == 1)
                continue;
            pq[pair<int, int>(u, v)] = 1;
            for(int j = 0; j < s3[u].size(); j++){
                int k = s3[u][j];
                if(must[k] == 1 && k != v)
                    s2[v].push_back(k);
            }
            for(int j = 0; j < s3[v].size(); j++){
                int k = s3[v][j];
                if(must[k] == 1 && k != u)
                    s2[u].push_back(k);
            }
        }
    }
    memset(low, -1, sizeof(low));
    for(int i = 1; i <= m; i++){
        if(low[i] == -1)
            scc(i);
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        if(Scc[u] == Scc[v] && r[i] == 0){
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int r[maxn], low[maxn], dfn[maxn], vis[maxn], q[maxn], now, num1, num2, Scc[maxn];
vector<int>  s1[maxn], s2[maxn], s3[maxn];
map<pair<int, int>, int> pq;
int must[maxn];
void scc(int x){
    low[x] = dfn[x] = ++now;
    vis[x] = 1;
    q[num1++] = x;
    for(int i = 0; i < s2[x].size(); i++){
        int u = s2[x][i];
        if(low[u] == -1){
            scc(u);
            low[x] = min(low[x], low[u]);
        }
        else if(vis[u] == 1){
            low[x] = min(low[x], dfn[u]);
        }
    }
    if(low[x] == dfn[x]){
        int m;
        do{
            m = q[num1 - 1];
            Scc[m] = num2;
            num1--;
            vis[m] = 0;
        }while(m != x);
        num2++;
    }
}
int main() {
    now = num1 = num2 = 0;
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &r[i]);
    for(int i = 1; i <= m; i++){
        scanf("%d", &x);
        for(int j = 0; j < x; j++){
            scanf("%d", &y);
            s1[y].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        if(r[i] == 1){
            s2[u].push_back(v);
            s2[v].push_back(u);
        }
        else{
            s3[u].push_back(v);
            s3[v].push_back(u);
        }
    }
    for(int i = 1; i <= n; i++){
        if(r[i] == 0){
            int u = s1[i][0];
            int v = s1[i][1];
            int t = min(u, v);
            u = max(u, v);
            v = t;
            if(pq[pair<int, int>(u, v)] == 1)
                continue;
            pq[pair<int, int>(u, v)] = 1;
            for(int j = 0; j < s3[u].size(); j++){
                int k = s3[u][j];
                if(k != v){
                    s2[v].push_back(k);
                    s2[k].push_back(v);
                }
            }
            for(int j = 0; j < s3[v].size(); j++){
                int k = s3[v][j];
                if(k != u){
                    s2[u].push_back(k);
                    s2[k].push_back(u);
                }
            }
        }
    }
    memset(low, -1, sizeof(low));
    for(int i = 1; i <= m; i++){
        if(low[i] == -1)
            scc(i);
    }
    for(int i = 1; i <= n; i++){
        int u = s1[i][0];
        int v = s1[i][1];
        if((Scc[u] == Scc[v] && r[i] == 0) || (Scc[u] != Scc[v] && r[i] == 1)){
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}