#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 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;
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 = 1;
    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 * 2].push_back(v * 2);
            s2[v * 2].push_back(u * 2);
            s2[u * 2 - 1].push_back(v *2 - 1);
            s2[v * 2 - 1].push_back(u * 2 - 1);
        }
        else{
            s2[u * 2].push_back(v * 2 - 1);
            s2[v * 2].push_back(u * 2 - 1);
            s2[u * 2 - 1].push_back(v * 2);
            s2[v * 2 - 1].push_back(u * 2);
        }
    }
    memset(low, -1, sizeof(low));
    for(int i = 1; i <= 2 * m; i++){
        if(low[i] == -1)
            scc(i);
    }
    for(int i = 1; i <= m; i++){
        if(Scc[i * 2] == Scc[i * 2 - 1]){
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define pb(a) push_back(a)
#define fir first
#define se second
#define LL long long
typedef pair<int,int> pii;
typedef pair<LL,int> pli;
const double eps = 0.0000001;
const LL mod = 1e9+7;
const int maxn = 1e5+5;
const LL dd = 2e14+5;

int r[maxn],c[maxn],vis[maxn],flag = 0;
vector<int> edge[maxn],g[maxn];
map<pii,int> mp;

void dfs(int u,int cc) {
	if(c[u] != -1 && c[u] != cc) flag = 1;
	else c[u] = cc;
	if(flag) return ;
	vis[u] = 1;
	for(int i = 0;i < g[u].size();i++) {
		int v = g[u][i];
		pii tmp;tmp.fir = u;tmp.se = v;
		int co = mp[tmp];
		if(c[v] != -1) {
			if(co && (c[u] != c[v])) flag = 1;
			else if(!co && (c[u] == c[v])) flag = 1;
		}
		if(!vis[v]) {
			if(co) dfs(v,cc);
			else dfs(v,(cc+1)%2);
		}
	}
	return ;
}

int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= n;i++) scanf("%d",&r[i]);
	for(int j = 1;j <= m;j++) {
		int x,s;
		scanf("%d",&x);
		for(int k = 1;k <= x;k++) {
			scanf("%d",&s);
			edge[s].pb(j);
		}
	}
	for(int i = 1;i <= n;i++) {
		int u = edge[i][0],v = edge[i][1];
		g[u].pb(v);
		g[v].pb(u);
		pii tmp1,tmp2;
		tmp1.fir = u,tmp1.se = v;
		tmp2.se = u,tmp2.fir = v;
		map<pii,int>::iterator it;
		it = mp.find(tmp1);
		if(it == mp.end()) {
			mp[tmp1] = mp[tmp2] = r[i];
		}
		else {
			int co = mp[tmp1];
			if(co != r[i]) flag = 1;
		}
	}
	/*printf("\n");
	for(int i = 1;i <= m;i++) {
		for(int j  = 0;j < g[i].size();j++) {
			printf("%d ",g[i][j]);
		}
		printf("\n");
	}*/
	clr(c,-1);
	for(int i = 1;i <= m;i++) {
		if(!vis[i]) {
			dfs(1,1);
		}
	}
	if(flag) printf("NO\n");
	else printf("YES\n");
}
/*
3 3
1 1 1
3 1 2 3
2 1 2
1 3
*/


#include<bits/stdc++.h>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define pb(a) push_back(a)
#define fir first
#define se second
#define LL long long
typedef pair<int,int> pii;
typedef pair<LL,int> pli;
const double eps = 0.0000001;
const LL mod = 1e9+7;
const int maxn = 1e5+5;
const LL dd = 2e14+5;

int r[maxn],c[maxn],vis[maxn],flag = 0;
vector<int> edge[maxn],g[maxn];
map<pii,int> mp;

void dfs(int u,int cc) {
	if(c[u] != -1 && c[u] != cc) flag = 1;
	else c[u] = cc;
	if(flag) return ;
	vis[u] = 1;
	for(int i = 0;i < g[u].size();i++) {
		int v = g[u][i];
		pii tmp;tmp.fir = u;tmp.se = v;
		int co = mp[tmp];
		if(c[v] != -1) {
			if(co && (c[u] != c[v])) flag = 1;
			else if(!co && (c[u] == c[v])) flag = 1;
			return ;
		}
		if(!vis[v]) {
			//printf("%d--%d %d\n",u,v,co);
			if(co) dfs(v,cc);
			else dfs(v,cc+1);
		}
	}
	return ;
}

int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= n;i++) scanf("%d",&r[i]);
	for(int j = 1;j <= m;j++) {
		int x,s;
		scanf("%d",&x);
		for(int k = 1;k <= x;k++) {
			scanf("%d",&s);
			edge[s].pb(j);
		}
	}
	for(int i = 1;i <= n;i++) {
		int u = edge[i][0],v = edge[i][1];
		g[u].pb(v);
		g[v].pb(u);
		pii tmp1,tmp2;
		tmp1.fir = u,tmp1.se = v;
		tmp2.se = u,tmp2.fir = v;
		map<pii,int>::iterator it;
		it = mp.find(tmp1);
		if(it == mp.end()) {
			mp[tmp1] = mp[tmp2] = r[i];
		}
		else {
			int co = mp[tmp1];
			if(co != r[i]) flag = 1;
		}
	}
	clr(c,-1);
	int cnt = 0;
	for(int i = 1;i <= n;i++) {
		if(!vis[i]) {
			cnt++;
			dfs(1,cnt);
		}
	}
	if(flag) printf("NO\n");
	else printf("YES\n");
}


#include<bits/stdc++.h>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define pb(a) push_back(a)
#define fir first
#define se second
#define LL long long
typedef pair<int,int> pii;
typedef pair<LL,int> pli;
const double eps = 0.0000001;
const LL mod = 1e9+7;
const int maxn = 1e5+5;
const LL dd = 2e14+5;

int r[maxn],c[maxn],vis[maxn],flag = 0;
vector<int> edge[maxn],g[maxn];
map<pii,int> mp;

void dfs(int u,int cc) {
	if(c[u] != -1 && c[u] != cc) flag = 1;
	else c[u] = cc;
	if(flag) return ;
	vis[u] = 1;
	for(int i = 0;i < g[u].size();i++) {
		int v = g[u][i];
		pii tmp;tmp.fir = u;tmp.se = v;
		int co = mp[tmp];
		if(c[v] != -1) {
			if(co && (c[u] != c[v])) flag = 1;
			else if(!co && (c[u] == c[v])) flag = 1;
			return ;
		}
		if(!vis[v]) {
			//printf("%d--%d %d\n",u,v,co);
			if(co) dfs(v,cc);
			else dfs(v,cc+1);
		}
	}
	return ;
}

int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= n;i++) scanf("%d",&r[i]);
	for(int j = 1;j <= m;j++) {
		int x,s;
		scanf("%d",&x);
		for(int k = 1;k <= x;k++) {
			scanf("%d",&s);
			edge[s].pb(j);
		}
	}
	for(int i = 1;i <= n;i++) {
		int u = edge[i][0],v = edge[i][1];
		g[u].pb(v);
		g[v].pb(u);
		pii tmp1,tmp2;
		tmp1.fir = u,tmp1.se = v;
		tmp2.se = u,tmp2.fir = v;
		map<pii,int>::iterator it;
		it = mp.find(tmp1);
		if(it == mp.end()) {
			mp[tmp1] = mp[tmp2] = r[i];
		}
		else {
			int co = mp[tmp1];
			if(co != r[i]) flag = 1;
		}
	}
	clr(c,-1);
	dfs(1,1);
	if(flag) printf("NO\n");
	else printf("YES\n");
}


#include<bits/stdc++.h>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define pb(a) push_back(a)
#define fir first
#define se second
#define LL long long
typedef pair<int,int> pii;
typedef pair<LL,int> pli;
const double eps = 0.0000001;
const LL mod = 1e9+7;
const int maxn = 1e5+5;
const LL dd = 2e14+5;

int r[maxn],c[maxn],vis[maxn],flag = 0;
vector<int> edge[maxn],g[maxn];
map<pii,int> mp;

void dfs(int u,int cc) {
	if(c[u] != -1 && c[u] != cc) flag = 1;
	else c[u] = cc;
	if(flag) return ;
	vis[u] = 1;
	for(int i = 0;i < g[u].size();i++) {
		int v = g[u][i];
		pii tmp;tmp.fir = u;tmp.se = v;
		int co = mp[tmp];
		if(c[v] != -1) {
			if(co && (c[u] != c[v])) flag = 1;
			else if(!co && (c[u] == c[v])) flag = 1;
		}
		if(!vis[v]) {
			if(co) dfs(v,cc);
			else dfs(v,(cc+1)%2);
		}
	}
	return ;
}

int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= n;i++) scanf("%d",&r[i]);
	for(int j = 1;j <= m;j++) {
		int x,s;
		scanf("%d",&x);
		for(int k = 1;k <= x;k++) {
			scanf("%d",&s);
			edge[s].pb(j);
		}
	}
	for(int i = 1;i <= n;i++) {
		int u = edge[i][0],v = edge[i][1];
		g[u].pb(v);
		g[v].pb(u);
		pii tmp1,tmp2;
		tmp1.fir = u,tmp1.se = v;
		tmp2.se = u,tmp2.fir = v;
		map<pii,int>::iterator it;
		it = mp.find(tmp1);
		if(it == mp.end()) {
			mp[tmp1] = mp[tmp2] = r[i];
		}
		else {
			int co = mp[tmp1];
			if(co != r[i]) flag = 1;
		}
	}
	/*printf("\n");
	for(int i = 1;i <= m;i++) {
		for(int j  = 0;j < g[i].size();j++) {
			printf("%d ",g[i][j]);
		}
		printf("\n");
	}*/
	clr(c,-1);
	for(int i = 1;i <= m;i++) {
		if(!vis[i]) {
			dfs(i,1);
		}
		/*else {
			for(int j = 0;j < g[i].size();j++) {
				int v = g[i][j];
				printf("%d-%d\n",i,v);
				pii tmp; tmp.fir = i,tmp.se = v;
				int co = mp[tmp];
				if(co && c[i] != c[v]) flag = 1;
				else if(!co && c[i] == c[v]) flag = 1;
			}
		}*/
	}
	if(flag) printf("NO\n");
	else printf("YES\n");
}
/*
3 3
1 1 1
3 1 2 3
2 1 2
1 3
*/


#include<bits/stdc++.h>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define pb(a) push_back(a)
#define fir first
#define se second
#define LL long long
typedef pair<int,int> pii;
typedef pair<LL,int> pli;
const double eps = 0.0000001;
const LL mod = 1e9+7;
const int maxn = 1e5+5;
const LL dd = 2e14+5;

int r[maxn],c[maxn],vis[maxn],flag = 0;
vector<int> edge[maxn],g[maxn];
map<pii,int> mp;

void dfs(int u,int cc) {
	if(c[u] != -1 && c[u] != cc) flag = 1;
	else c[u] = cc;
	if(flag) return ;
	vis[u] = 1;
	for(int i = 0;i < g[u].size();i++) {
		int v = g[u][i];
		pii tmp;tmp.fir = u;tmp.se = v;
		int co = mp[tmp];
		if(c[v] != -1) {
			if(co && (c[u] != c[v])) flag = 1;
			else if(!co && (c[u] == c[v])) flag = 1;
		}
		if(!vis[v]) {
			if(co) dfs(v,cc);
			else dfs(v,cc+1);
		}
	}
}

int main() {
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= n;i++) scanf("%d",&r[i]);
	for(int j = 1;j <= m;j++) {
		int x,s;
		scanf("%d",&x);
		for(int k = 1;k <= x;k++) {
			scanf("%d",&s);
			edge[s].pb(j);
		}
	}
	for(int i = 1;i <= n;i++) {
		int u = edge[i][0],v = edge[i][1];
		g[u].pb(v);
		g[v].pb(u);
		pii tmp1,tmp2;
		tmp1.fir = u,tmp1.se = v;
		tmp2.se = u,tmp2.fir = v;
		map<pii,int>::iterator it;
		it = mp.find(tmp1);
		if(it == mp.end()) {
			mp[tmp1] = mp[tmp2] = r[i];
		}
		else {
			int co = mp[tmp1];
			if(co != r[i]) flag = 1;
		}
	}
	/*printf("\n");
	for(int i = 1;i <= m;i++) {
		for(int j  = 0;j < g[i].size();j++) {
			printf("%d ",g[i][j]);
		}
		printf("\n");
	}*/
	clr(c,-1);
	int cnt = 0;
	for(int i = 1;i <= m;i++) {
		if(!vis[i]) {
			cnt++;
			dfs(1,cnt);
		}
	}
	if(flag) printf("NO\n");
	else printf("YES\n");
}
/*
3 3
1 1 1
3 1 2 3
2 1 2
1 3
*/


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = z;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i + n;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return a[x];
	}
	int setRoot(int index, int val)
	{
		a[index] = val;
	}
	// void showDsu()
	// {
	// 	f(i, 0, n)
	// 	{
	// 		//cout<<a[i]<<" ";
	// 	}
	// 	//cout<<endl;
	// }
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && min(a, b) % 2 == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	// //cout<<"In done"<<endl;
	Dsu d;
	d.build(m);
	int maxSet = 0, g, h;
	f(i, 0, n)
	{
		//cout<<"i = "<<i<<endl;
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		//cout<<"g = "<<g<<" h = "<<h<<endl;
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g < m && h < m)
				{
					if(isPair(g, h))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g < m)
				{
					d.setRoot(a[i][1], g);
				}
				else if(h < m)
				{
					d.setRoot(a[i][0], h);
				}
				else
				{
					d.setRoot(a[i][0], maxSet);
					d.setRoot(a[i][1], maxSet);
					maxSet++;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g < m && h < m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g < m)
			{
				d.setRoot(a[i][1], getOpposite(g));
			}
			else if(h < m)
			{
				d.setRoot(a[i][0], getOpposite(h));
			}
			else
			{
				d.setRoot(a[i][0], maxSet);
				maxSet++;
				d.setRoot(a[i][1], maxSet);
				maxSet++;
			}
		}
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = 4 * z;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return findRoot(x);
	}
	void showDsu()
	{
		f(i, 0, n)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && min(a, b) % 2 == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	// //cout<<"In done"<<endl;
	Dsu d;
	d.build(m);
	int maxSet = 2 * m, g, h;
	f(i, 0, n)
	{
		// cout<<"i = "<<i<<endl;
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		// cout<<"g = "<<g<<" h = "<<h<<endl;
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g >= m && h >= m)
				{
					if(isPair(g, h))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g >= m)
				{
					d.merge(a[i][1], g);
				}
				else if(h >= m)
				{
					d.merge(a[i][0], h);
				}
				else
				{
					d.merge(a[i][0], maxSet);
					d.merge(a[i][1], maxSet);
					maxSet++;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g >= m && h >= m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g >= m)
			{
				d.merge(a[i][1], getOpposite(g));
			}
			else if(h >= m)
			{
				d.merge(a[i][0], getOpposite(h));
			}
			else
			{
				d.merge(a[i][0], maxSet);
				maxSet++;
				d.merge(a[i][1], maxSet);
				maxSet++;
			}
		}
		// d.showDsu();
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = 4 * z;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return findRoot(x);
	}
	void showDsu()
	{
		f(i, 0, n)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && min(a, b) % 2 == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	// //cout<<"In done"<<endl;
	Dsu d;
	d.build(m);
	int maxSet = 2 * m, g, h;
	f(i, 0, n)
	{
		// cout<<"i = "<<i<<endl;
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		// cout<<"g = "<<g<<" h = "<<h<<endl;
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g >= m && h >= m)
				{
					if(isPair(h, g))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g >= m)
				{
					d.merge(a[i][1], g);
				}
				else if(h >= m)
				{
					d.merge(a[i][0], h);
				}
				else
				{
					d.merge(a[i][0], maxSet);
					d.merge(a[i][1], maxSet);
					maxSet++;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g >= m && h >= m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g >= m)
			{
				d.merge(a[i][1], getOpposite(g));
			}
			else if(h >= m)
			{
				d.merge(a[i][0], getOpposite(h));
			}
			else
			{
				d.merge(a[i][0], maxSet);
				maxSet++;
				d.merge(a[i][1], maxSet);
				maxSet++;
			}
		}
		// d.showDsu();
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = 4 * z;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return findRoot(x);
	}
	void showDsu()
	{
		f(i, 0, n)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && min(a, b) % 2 == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	// //cout<<"In done"<<endl;
	Dsu d;
	d.build(m);
	int maxSet = 2 * m, g, h;
	if(maxSet % 2 == 1)
		maxSet++;
	f(i, 0, n)
	{
		// cout<<"i = "<<i<<endl;
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		// cout<<"g = "<<g<<" h = "<<h<<endl;
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g >= m && h >= m)
				{
					if(isPair(g, h))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g >= m)
				{
					d.merge(a[i][1], g);
				}
				else if(h >= m)
				{
					d.merge(a[i][0], h);
				}
				else
				{
					d.merge(a[i][0], maxSet);
					d.merge(a[i][1], maxSet);
					maxSet++;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g >= m && h >= m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g >= m)
			{
				d.merge(a[i][1], getOpposite(g));
			}
			else if(h >= m)
			{
				d.merge(a[i][0], getOpposite(h));
			}
			else
			{
				d.merge(a[i][0], maxSet);
				maxSet++;
				d.merge(a[i][1], maxSet);
				maxSet++;
			}
		}
		// d.showDsu();
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = 6 * z;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		findRoot(x);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return findRoot(x);
	}
	void showDsu()
	{
		f(i, 0, n)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && (min(a, b) % 2) == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	// //cout<<"In done"<<endl;
	Dsu d;
	d.build(m);
	int maxSet = 3 * m, g, h;
	if(maxSet % 2 == 1)
		maxSet--;
	f(i, 0, n)
	{
		// cout<<"i = "<<i<<endl;
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		// cout<<"g = "<<g<<" h = "<<h<<endl;
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g >= m && h >= m)
				{
					if(isPair(g, h))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g >= m)
				{
					d.merge(a[i][1], g);
				}
				else if(h >= m)
				{
					d.merge(a[i][0], h);
				}
				else
				{
					d.merge(a[i][0], maxSet);
					d.merge(a[i][1], maxSet);
					maxSet++;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g >= m && h >= m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g >= m)
			{
				d.merge(a[i][1], getOpposite(g));
			}
			else if(h >= m)
			{
				d.merge(a[i][0], getOpposite(h));
			}
			else
			{
				d.merge(a[i][0], maxSet);
				maxSet++;
				d.merge(a[i][1], maxSet);
				maxSet++;
			}
		}
		// d.showDsu();
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = 4 * z;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return a[x];
	}
	int setRoot(int index, int val)
	{
		a[index] = val;
	}
	void showDsu()
	{
		f(i, 0, n)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && min(a, b) % 2 == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	// //cout<<"In done"<<endl;
	Dsu d;
	d.build(m);
	int maxSet = 2 * m, g, h;
	f(i, 0, n)
	{
		// cout<<"i = "<<i<<endl;
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		// cout<<"g = "<<g<<" h = "<<h<<endl;
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g >= m && h >= m)
				{
					if(isPair(g, h))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g >= m)
				{
					d.merge(a[i][1], g);
				}
				else if(h >= m)
				{
					d.merge(a[i][0], h);
				}
				else
				{
					d.merge(a[i][0], maxSet);
					d.merge(a[i][1], maxSet);
					maxSet++;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g >= m && h >= m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g >= m)
			{
				d.merge(a[i][1], getOpposite(g));
			}
			else if(h >= m)
			{
				d.merge(a[i][0], getOpposite(h));
			}
			else
			{
				d.merge(a[i][0], maxSet);
				maxSet++;
				d.merge(a[i][1], maxSet);
				maxSet++;
			}
		}
		// d.showDsu();
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = 6 * z;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		findRoot(x);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return findRoot(x);
	}
	void showDsu()
	{
		f(i, 0, n)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && (min(a, b) % 2) == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	// //cout<<"In done"<<endl;
	Dsu d;
	d.build(m);
	int maxSet = 3 * m, g, h;
	if(maxSet % 2 == 1)
		maxSet--;
	f(i, 0, n)
	{
		// cout<<"i = "<<i<<endl;
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		// cout<<"g = "<<g<<" h = "<<h<<endl;
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g >= m && h >= m)
				{
					if(isPair(g, h))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g >= m)
				{
					d.merge(a[i][1], g);
				}
				else if(h >= m)
				{
					d.merge(a[i][0], h);
				}
				else
				{
					d.merge(a[i][0], maxSet);
					d.merge(a[i][1], maxSet);
					maxSet+=2;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g >= m && h >= m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g >= m)
			{
				d.merge(a[i][1], getOpposite(g));
			}
			else if(h >= m)
			{
				d.merge(a[i][0], getOpposite(h));
			}
			else
			{
				d.merge(a[i][0], maxSet);
				maxSet++;
				d.merge(a[i][1], maxSet);
				maxSet++;
			}
		}
		// d.showDsu();
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = 6 * z;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return findRoot(x);
	}
	void showDsu()
	{
		f(i, 0, n)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && min(a, b) % 2 == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	// //cout<<"In done"<<endl;
	Dsu d;
	d.build(m);
	int maxSet = 3 * m, g, h;
	if(maxSet % 2 == 1)
		maxSet++;
	f(i, 0, n)
	{
		// cout<<"i = "<<i<<endl;
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		// cout<<"g = "<<g<<" h = "<<h<<endl;
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g >= m && h >= m)
				{
					if(isPair(g, h))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g >= m)
				{
					d.merge(a[i][1], g);
				}
				else if(h >= m)
				{
					d.merge(a[i][0], h);
				}
				else
				{
					d.merge(a[i][0], maxSet);
					d.merge(a[i][1], maxSet);
					maxSet++;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g >= m && h >= m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g >= m)
			{
				d.merge(a[i][1], getOpposite(g));
			}
			else if(h >= m)
			{
				d.merge(a[i][0], getOpposite(h));
			}
			else
			{
				d.merge(a[i][0], maxSet);
				maxSet++;
				d.merge(a[i][1], maxSet);
				maxSet++;
			}
		}
		// d.showDsu();
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = (3 * z) + 1;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		findRoot(x);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return findRoot(x);
	}
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && (min(a, b) % 2) == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	Dsu d;
	d.build(m);
	int maxSet = m, g, h;
	if(maxSet % 2 == 1)
		maxSet++;
	f(i, 0, n)
	{
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g >= m && h >= m)
				{
					if(isPair(g, h))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g >= m)
				{
					d.merge(a[i][1], g);
				}
				else if(h >= m)
				{
					d.merge(a[i][0], h);
				}
				else
				{
					d.merge(a[i][0], maxSet);
					d.merge(a[i][1], maxSet);
					maxSet+=2;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g >= m && h >= m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g >= m)
			{
				d.merge(a[i][1], getOpposite(g));
			}
			else if(h >= m)
			{
				d.merge(a[i][0], getOpposite(h));
			}
			else
			{
				d.merge(a[i][0], maxSet);
				maxSet++;
				d.merge(a[i][1], maxSet);
				maxSet++;
			}
		}
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pt;
typedef pair<long long ,long long> ptl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef vector<pair<int, int>> vpt;
typedef vector<pair<long long, long long>> vptl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef unordered_set <int> usi;
typedef unordered_set <long long> usl;
typedef multiset<int> msi;
typedef multiset<long long> msl;
typedef map<int, int> mi;
typedef map<long long, long long> ml;
template <typename T>
using indexed_set = tree<T, null_type, less<T>,
	rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key: The number of items in a set that are strictly smaller than k
// find_by_order: It returns an iterator to the ith largest element
//Anubhaw Bhalotia https://github.com/anubhawbhalotia
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) x.begin(), x.end()
#define f(i,s,n) for(int i=s;i<n;i++)
#define fe(i,s,n) for(int i=s;i<=n;i++)
#define fr(i,s,n) for(int i=s;i>n;i--)
#define fre(i,s,n) for(int i=s;i>=n;i--)
const int MOD = 998244353;
class Dsu
{
private:
	vi a;
	int n;
	int findRoot(int x)
	{
		vi temp;
		while(a[x] != x)
		{
			temp.pb(x);
			x = a[x];
		}
		for(auto i : temp)
		{
			a[i] = x;
		}
		return x;
	}
public:
	int build(int z)
	{
		n = 6 * z;
		a.resize(n);
		f(i, 0, n)
		{
			a[i] = i;
		}
		return 1;
	}
	int merge(int x, int y)
	{
		int temp = findRoot(y);
		a[findRoot(x)] = findRoot(y);
		findRoot(x);
		return temp;
	}
	bool isSameSet(int x, int y)
	{
		return findRoot(x) == findRoot(y);
	}
	int getSet(int x)
	{
		return findRoot(x);
	}
	void showDsu()
	{
		f(i, 0, n)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}
};
bool isPair(int a, int b)
{
	if(abs(a - b) == 1 && min(a, b) % 2 == 0)
		return 1;
	return 0;
}
int getOpposite(int a)
{
	if(a % 2 == 0)
		return a + 1;
	else
		return a - 1;
}
int solution(int tc)
{
	int n, m;
	cin>>n>>m;
	vi r(n);
	f(i, 0, n)
	{
		cin>>r[i];
	}
	int x, y;
	vvi a(n);
	f(i, 0, m)
	{
		cin>>x;
		f(j, 0, x)
		{
			cin>>y;
			a[--y].pb(i);
		}
	}
	// //cout<<"In done"<<endl;
	Dsu d;
	d.build(m);
	int maxSet = 3 * m, g, h;
	if(maxSet % 2 == 1)
		maxSet++;
	f(i, 0, n)
	{
		// cout<<"i = "<<i<<endl;
		g = d.getSet(a[i][0]);
		h = d.getSet(a[i][1]);
		// cout<<"g = "<<g<<" h = "<<h<<endl;
		if(r[i] == 1)
		{
			if(g != h)
			{
				if(g >= m && h >= m)
				{
					if(isPair(g, h))
					{
						return 0;
					}
					d.merge(h, g);
					d.merge(getOpposite(h), getOpposite(g));
				}
				else if(g >= m)
				{
					d.merge(a[i][1], g);
				}
				else if(h >= m)
				{
					d.merge(a[i][0], h);
				}
				else
				{
					d.merge(a[i][0], maxSet);
					d.merge(a[i][1], maxSet);
					maxSet++;
				}
			}
		}
		else
		{
			if(g == h)
			{
				return 0;
			}
			if(g >= m && h >= m)
			{
				if(!isPair(g, h))
				{
					d.merge(h, getOpposite(g));
					d.merge(getOpposite(h), g);
				}
			}
			else if(g >= m)
			{
				d.merge(a[i][1], getOpposite(g));
			}
			else if(h >= m)
			{
				d.merge(a[i][0], getOpposite(h));
			}
			else
			{
				d.merge(a[i][0], maxSet);
				maxSet++;
				d.merge(a[i][1], maxSet);
				maxSet++;
			}
		}
		// d.showDsu();
	}
	return 1;
}
void testCase()
{
	int tc = 1;
	// cin>>tc;
	map <bool, string> m;
	m[1] = "YES";
	m[0] = "NO";
	f(i, 0, tc)
	{
		bool ans = solution(i + 1);
		cout<<m[ans]<<endl;
		// //cout<<"Case #"<<i + 1<<": "<<solution(i + 1)<<endl;
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    testCase();
}


#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for (int i=s;i<e;i++)
#define FOE(i,s,e) for (int i=s;i<=e;i++)
#define FOD(i,s,e) for (int i=s;i>=e;i--)
#define SET(a,e) memset(a,e,sizeof(a))
#define LL long long
#define LD long double
#define pb push_back
#define x first
#define y second
#define PII pair<int,int>
#define PLI pair<LL,int>
#define PIL pair<int,LL>
#define PLL pair<LL,LL>
#define PDD pair<LD,LD>
#define eps 1e-9
#define HH1 402653189
#define HH2 1610612741

int n, m, r[100055], x, y, z, state[100055];
vector<int> key[100055];
vector<PII> e[100055];

void dfs(int at, int ver) {
	state[at] = ver;

	FOR(i, 0, e[at].size()) {
		int to = e[at][i].x;
		int same = e[at][i].y;
		if (state[to] == -1) {
			if (same) dfs(to, ver);
			else dfs(to, 1 - ver);
		} else {
			if (same && state[at] != state[to]) {
				printf("NO\n");
				exit(0);
			}
			else if (!same && state[at] == state[to]) {
				printf("NO\n");
				exit(0);
			}
		}
	}
}

int main () {

	scanf("%d%d", &n, &m);

	FOE(i, 1, n) scanf("%d", r + i);

	FOE(i, 1, m) {
		scanf("%d", &x);
		while (x--) {
			scanf("%d", &y);
			key[y].pb(i);
		}
	}

	FOE(i, 1, n) {
	    if (key[i].size() != 2) return 1;
		x = key[i][0];
		y = key[i][1];
		e[x].pb((PII){y, r[i]});
		e[y].pb((PII){x, r[i]});
	}

	SET(state, -1);

	FOE(i, 1, n) if (state[i] == -1) {
		dfs(i, 0);
	}

	printf("YES\n");

	return 0;

}
close


#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for (int i=s;i<e;i++)
#define FOE(i,s,e) for (int i=s;i<=e;i++)
#define FOD(i,s,e) for (int i=s;i>=e;i--)
#define SET(a,e) memset(a,e,sizeof(a))
#define LL long long
#define LD long double
#define pb push_back
#define x first
#define y second
#define PII pair<int,int>
#define PLI pair<LL,int>
#define PIL pair<int,LL>
#define PLL pair<LL,LL>
#define PDD pair<LD,LD>
#define eps 1e-9
#define HH1 402653189
#define HH2 1610612741

int n, m, r[100055], x, y, z, state[100055];
vector<int> key[100055];
vector<PII> e[100055];

void dfs(int at, int ver) {
	state[at] = ver;

	FOR(i, 0, e[at].size()) {
		int to = e[at][i].x;
		int same = e[at][i].y;
		if (state[to] == -1) {
			if (same) dfs(to, ver);
			else dfs(to, 1 - ver);
		} else {
			if (same && state[at] != state[to]) {
				printf("NO\n");
				exit(0);
			}
			else if (!same && state[at] == state[to]) {
				printf("NO\n");
				exit(0);
			}
		}
	}
}

int main () {

	scanf("%d%d", &n, &m);

	FOE(i, 1, n) scanf("%d", r + i);

	FOE(i, 1, m) {
		scanf("%d", &x);
		while (x--) {
			scanf("%d", &y);
			key[y].pb(i);
		}
	}

	FOE(i, 1, n) {
		x = key[i][0];
		y = key[i][1];
		e[x].pb((PII){y, r[i]});
		e[y].pb((PII){x, r[i]});
	}

	SET(state, -1);

	FOE(i, 1, m) if (state[i] == -1) {
		dfs(i, 0);
	}

	printf("YES\n");

	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for (int i=s;i<e;i++)
#define FOE(i,s,e) for (int i=s;i<=e;i++)
#define FOD(i,s,e) for (int i=s;i>=e;i--)
#define SET(a,e) memset(a,e,sizeof(a))
#define LL long long
#define LD long double
#define pb push_back
#define x first
#define y second
#define PII pair<int,int>
#define PLI pair<LL,int>
#define PIL pair<int,LL>
#define PLL pair<LL,LL>
#define PDD pair<LD,LD>
#define eps 1e-9
#define HH1 402653189
#define HH2 1610612741

int n, m, r[100055], x, y, z, state[100055];
vector<int> key[100055];
vector<PII> e[100055];

void dfs(int at, int ver) {
	state[at] = ver;

	FOR(i, 0, e[at].size()) {
		int to = e[at][i].x;
		int same = e[at][i].y;
		if (state[to] == -1) {
			if (same) dfs(to, ver);
			else dfs(to, 1 - ver);
		} else {
			if (same && state[at] != state[to]) {
				printf("NO\n");
				exit(0);
			}
			else if (!same && state[at] == state[to]) {
				printf("NO\n");
				exit(0);
			}
		}
	}
}

int main () {

	scanf("%d%d", &n, &m);

	FOE(i, 1, n) scanf("%d", r + i);

	FOE(i, 1, m) {
		scanf("%d", &x);
		while (x--) {
			scanf("%d", &y);
			key[y].pb(i);
		}
	}

	FOE(i, 1, n) {
		x = key[i][0];
		y = key[i][1];
		e[x].pb((PII){y, r[i]});
		e[y].pb((PII){x, r[i]});
	}

	SET(state, -1);

	FOE(i, 1, n) if (state[i] == -1) {
		dfs(i, 0);
	}

	printf("YES\n");

	return 0;


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<ctime>
#include<vector>
#include<cmath>
#include<algorithm>
#define ll long long
#define For(i,j,k)	for(ll i=j;i<=k;i++)
#define Dow(i,j,k)	for(ll i=k;i>=j;i--)
using namespace std;
inline void read(ll &tx){   ll x=0,f=1;char ch=getchar();   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}  while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}  tx=x*f; }
inline void write(ll x){    if (x<0) putchar('-'),x=-x; if (x>=10) write(x/10);   putchar(x%10+'0');  }
inline void writeln(ll x){write(x);puts("");}
ll n,son[1000001][2],q[1000001],cnt[1000001],tot;
char c,s[21];
inline void add(char s[],ll k)
{
	ll top=0;ll len=strlen(s+1);
	Dow(i,1,len)	q[++top]=(s[i]-'0')%2;
	ll now=0;
	For(i,1,32)
	{
		if(!son[now][q[i]])	son[now][q[i]]=++tot;
		cnt[now]+=k;
		now=son[now][q[i]];
	}
	cnt[now]+=k;
}
inline ll ask(char s[])
{
	ll top=0;ll len=strlen(s+1);
	Dow(i,1,len)	q[++top]=s[i]-'0';
	ll now=0;
	For(i,1,len)
	{
		if(!son[now][q[i]])	return 0;
		now=son[now][q[i]];
	}
	return cnt[now];
}
int main()
{
	scanf("%I64d",&n);
	For(i,1,n)
	{
		scanf("\n%c",&c);
		if(c=='+'){scanf("%s",s+1);add(s,1);}
		if(c=='-'){scanf("%s",s+1);add(s,-1);}
		if(c=='?'){scanf("%s",s+1);writeln(ask(s));}
	}
}


#include <bits/stdc++.h>
#define si( x ) scanf("%d", &x)
#define sll( x ) scanf("%I64d", &x)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define INF 1000000000
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll s;
int t,x[2<<18],k;
map<ll,int> ma;
char c;
string ch;
bool u[18];

void fuck(ll y){
	for(int i=17;i>=0;i--){
		if(y%2) u[i]=1;
		y/=10;
	}
}

int cc(){
	k=0;
	for(int i=0;i<=17;i++){
		k*=2;
		k+= (u[i]) ? 1:0;
	}
	return k;
}

int main(){
	int i,j;
	si(t);
	while(t--){
		memset(u, 0, sizeof u);
		k=0;
		cin >> c;
		if(c=='+'){
			sll(s);
			ma[s]++;
			fuck(s);
			x[cc()]++;
		}
		if(c=='-'){
			sll(s);
			ma[s]--;
			fuck(s);
			x[cc()]--;
		}
		if(c=='?'){
			cin >> ch;
			j=17;
			for(i=ch.size()-1;i>=0;i--) u[j--]= (ch[i]=='1') ? 1:0;
			printf("%d\n",x[cc()]);
		}
	}
}


#include <bits/stdc++.h>
#define si( x ) scanf("%d", &x)
#define sll( x ) scanf("%I64d", &x)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define INF 1000000000
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll s;
int t,x[2<<18],k;
char c;
string ch;
bool u[18];

void fuck(ll y){
	for(int i=17;i>=0;i--){
		if(y%2) u[i]=1;
		y/=10;
	}
}

int cc(){
	k=0;
	for(int i=0;i<=17;i++){
		k*=2;
		k+= (u[i]) ? 1:0;
	}
	return k;
}

int main(){
	int i,j;
	si(t);
	while(t--){
		memset(u, 0, sizeof u);
		k=0;
		cin >> c;
		if(c=='?'){
			cin >> ch;
			j=17;
			for(i=ch.size()-1;i>=0;i--) u[j--]= (ch[i]=='1') ? 1:0;
			printf("%d\n",x[cc()]);
		}
		else{
			sll(s);
			fuck(s);
			x[cc()]+= (c=='+') ? 1:-1;
		}
	}
}


#include <bits/stdc++.h>
#define si( x ) scanf("%d", &x)
#define sll( x ) scanf("%I64d", &x)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define INF 1000000000
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll s;
int t,x[2<<18],k;
char c;
string ch;
bool u[18];

void fuck(ll y){
	for(int i=17;i>=0;i--){
		if(y%2) u[i]=1;
		y/=10;
	}
}

int cc(){
	k=0;
	for(int i=0;i<=17;i++){
		k*=2;
		k+= (u[i]) ? 1:0;
	}
	return k;
}

int main(){
	int i,j;
	si(t);
	while(t--){
		memset(u, 0, sizeof u);
		k=0;
		cin >> c;
		if(c=='+'){
			sll(s);
			fuck(s);
			x[cc()]++;
		}
		if(c=='-'){
			sll(s);
			fuck(s);
			x[cc()]--;
		}
		if(c=='?'){
			cin >> ch;
			j=17;
			for(i=ch.size()-1;i>=0;i--) u[j--]= (ch[i]=='1') ? 1:0;
			printf("%d\n",x[cc()]);
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()

#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;

    vvll app(18, vll(1 << 18, 0));

    for (int i = 0; i < N; i++) {
        char op;
        cin >> op;

        if (op == '?') {
            string query;
            cin >> query;

            int l = query.size();
            ll qval = 0;
            for (char c : query) {
                qval = (2 * qval) + (c == '1');
            }

            cout << app[l - 1][qval] << "\n";
        } else {
            ll val;
            cin >> val;

            int diff = (op == '+') ? 1 : -1;
            ll rep = 0, tmp = val;

            for (int l = 0; l < 18; l++) {
                ll bit = (tmp % 10) % 2;
                tmp /= 10;
                rep += bit << l;

                app[l][rep] += diff;
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()

#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;

    vll app(1 << 18);

    for (int i = 0; i < N; i++) {
        char op;
        cin >> op;

        if (op == '?') {
            string query;
            cin >> query;

            ll qval = 0;
            for (char c : query) {
                qval = (2 * qval) + (c == '1');
            }

            cout << app[qval] << "\n";
        } else {
            ll val;
            cin >> val;

            int diff = (op == '+') ? 1 : -1;
            ll rep = 0, tmp = val;

            for (int l = 0; l < 18; l++) {
                ll bit = (tmp % 10) % 2;
                tmp /= 10;
                rep += bit << l;
            }

            app[rep] += diff;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()

#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;

    vvll app(18, vll(1 << 16, 0));

    for (int i = 0; i < N; i++) {
        char op;
        cin >> op;

        if (op == '?') {
            string query;
            cin >> query;

            int l = query.size();
            ll qval = 0;
            for (char c : query) {
                qval = (2 * qval) + (c == '1');
            }

            cout << app[l][qval] << "\n";
        } else {
            ll val;
            cin >> val;

            int diff = (op == '+') ? 1 : -1;
            ll rep = 0, tmp = val;

            for (int l = 0; l < 18; l++) {
                ll bit = (tmp % 10) % 2;
                tmp /= 10;
                rep += bit << l;

                app[l][rep] += diff;
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()

#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;

    vvll app(18, vll(1 << 18, 0));

    for (int i = 0; i < N; i++) {
        char op;
        cin >> op;

        if (op == '?') {
            string query;
            cin >> query;

            int l = query.size();
            ll qval = 0;
            for (char c : query) {
                qval = (2 * qval) + (c == '1');
            }

            cout << app[l][qval] << "\n";
        } else {
            ll val;
            cin >> val;

            int diff = (op == '+') ? 1 : -1;
            ll rep = 0, tmp = val;

            for (int l = 0; l < 18; l++) {
                ll bit = (tmp % 10) % 2;
                tmp /= 10;
                rep += bit << l;

                app[l][rep] += diff;
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()

#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;

    vll app(1 << 18);

    for (int i = 0; i < N; i++) {
        char op;
        cin >> op;

        if (op == '?') {
            string query;
            cin >> query;

            ll qval = 0;
            for (char c : query) {
                qval = (2 * qval) + (c == '1');
            }

            cout << app[qval] << "\n";
        } else {
            ll val;
            cin >> val;

            int diff = (op == '+') ? 1 : -1;
            ll rep = 0, tmp = val;

            for (int l = 0; l < 18; l++) {
                ll bit = (tmp % 10) % 2;
                tmp /= 10;
                rep += bit << l;
            }

            app[rep]++;
        }
    }

    return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>

#include<string.h>
using namespace std;
const int maxn = 262144+2;
int num[maxn];
int main()
{

	int T;
	cin>>T;
	while (T--)
	{
		char c;
	    int num_1;
		cin >> c >> num_1;
	    int num_2 = 0;
		for (int i = 0; i < 18; i++)
		{
			num_2 = num_2 * 2 + num_1 % 2;
			num_1 = num_1 / 10;
		}
		if (c == '+')
			num[num_2]++;
		else if (c == '-')
			num[num_2]--;
		else if (c == '?')
			cout << num[num_2] << endl;
	}
	return 0;
}


#include<iostream>
#include<map>
#include<string.h>
using namespace std;

int main()
{

	int T;
	cin>>T;
	map<long int, int>num;

	while (T--)
	{
		char c;
	    long int num_1;
		cin >> c >> num_1;
		long int num_2 = 0;
		for (int i = 0; i<18; i++)
		{
			num_2 = num_2 * 2 + num_1 % 2;
			num_1 = num_1 / 10;
		}
		if (c == '+')
			num[num_2]++;
		else if (c == '-')
			num[num_2]--;
		else if (c == '?')
			cout<<num[num_2]<<endl;
	}
	return 0;
}


#include<iostream>
#include<map>
#include<string.h>
using namespace std;

int main()
{

	int T;
	cin>>T;
	map<long long int, int>num;

	while (T--)
	{
		char c;
	    long long int num_1;
		cin >> c >> num_1;
		long long int num_2 = 0;
		for (int i = 0; i<18; i++)
		{
			num_2 = num_2 * 2 + num_1 % 2;
			num_1 = num_1 / 10;
		}
		if (c == '+')
			num[num_2]++;
		else if (c == '-')
			num[num_2]--;
		else if (c == '?')
			cout<<num[num_2]<<endl;
	}
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#include<string.h>
using namespace std;

int main()
{

	int T;
	cin>>T;
	map<long long int, long long int>num;

	while (T--)
	{
		char c;
		long long int num_1;
		cin >> c >> num_1;
		long long int num_2 = 0;
		for (int i = 0; i<18; i++)
		{
			num_2 = num_2 * 2 + num_1 % 2;
			num_1 = num_1 / 10;
		}
		if (c == '+')
			num[num_2]++;
		else if (c == '-')
			num[num_2]--;
		else if (c == '?')
			cout<<num[num_2]<<endl;
	}
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>

#include<string.h>
using namespace std;
const int maxn = 262144;
int num[maxn];
int main()
{

	int T;
	cin>>T;
	while (T--)
	{
		char c;
	    int num_1;
		cin >> c >> num_1;
	    int num_2 = 0;
		for (int i = 0; i < 18; i++)
		{
			num_2 = num_2 * 2 + num_1 % 2;
			num_1 = num_1 / 10;
		}
		if (c == '+')
			num[num_2]++;
		else if (c == '-')
			num[num_2]--;
		else if (c == '?')
			cout << num[num_2] << endl;
	}
	return 0;
}


#include<iostream>
#include<map>
#include<string.h>
using namespace std;

int main()
{

	int T;
	cin>>T;
	map<long long int, int>num;

	while (T--)
	{
		char c;
		long long int num_1;
		cin >> c >> num_1;
		long long int num_2 = 0;
		for (int i = 0; i<18; i++)
		{
			num_2 = num_2 * 2 + num_1 % 2;
			num_1 = num_1 / 10;
		}
		if (c == '+')
			num[num_2]++;
		else if (c == '-')
			num[num_2]--;
		else if (c == '?')
			cout<<num[num_2]<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

//#define TT

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int maxn = 1000000;
const int MOD = 1e9+7;

int bintree[maxn];

void init()
{
    memset(bintree,-1,sizeof(bintree));
}

bool isleave(int p)
{
    return bintree[p<<1] == -1 && bintree[(p<<1)+1] == -1;
}

void add(ll x)
{
    int p=1;
    while(x)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        int tmp = x % 10;
        if(tmp&1)
        {
            p = rson;
        }
        else
        {
            p = lson;
        }

        if(bintree[p]==-1)
        {
            bintree[p] = 0;
        }

        if(tmp == x)
        {
            bintree[p]++;
        }
        x /= 10;
    }
}

int getit(char ss[])
{
    int res=0;
    int p = 1;
    int len = strlen(ss);
    int flag=0;
    for(int i=len-1;i>=0;i--)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        if(ss[i] == '1')
        {
            p = rson;
        }
        else
        {
            p = lson;
        }
        if(bintree[p] == -1 && ss[i] == '1') return 0;
        if(i == 0)
        {
            res += bintree[p];
           // flag = 1;
        }
    }
    while(p<maxn)
    {
        p <<= 1;
        if(bintree[p] == -1)
        {
            break;
        }
        res += bintree[p];
    }
    return res;
}

void del(ll x)
{
    int p=1;
    while(x)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        int tmp = x % 10;
        if(tmp&1)
        {
            p = rson;
        }
        else
        {
            p = lson;
        }

        if(bintree[p]==-1)
        {
            return;
        }

        if(tmp == x)
        {
            bintree[p]--;
        }
        x /= 10;
    }

    while(bintree[p]==0 && isleave(p))
    {
        bintree[p] = -1;
        p >>= 1;
    }
}

void work()
{
    init();
    int n;
    scanf("%d",&n);
    while(n--)
    {
        getchar();
        char c;
        scanf("%c",&c);
        if(c=='+')
        {
            ll x;
            scanf("%I64d",&x);
            add(x);
        }
        else if(c=='-')
        {
            ll x;
            scanf("%I64d",&x);
            del(x);
        }
        else
        {
            char s[22],ss[22];
            scanf("%s",s);
            int st=0;
            while(s[st] == '0' && s[st]) st++;
            int j=0;
            for(int i = st; s[i]; i++, j++)
                ss[j]=s[i];
            ss[j] = '\0';
          //  cout<<ss<<endl;
            int ans = getit(ss);
            printf("%d\n",ans >= 0 ? ans : 0);
        }
    }
}

int main()
{
#ifdef TT
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        printf("Case #%d: ",ca);
        work();
    }
#else
    work();
#endif // TT
}


#include <bits/stdc++.h>

//#define TT

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int maxn = 550000;
const int MOD = 1e9+7;

int bintree[maxn];

void init()
{
    memset(bintree,-1,sizeof(bintree));
}

bool isleave(int p)
{
    return bintree[p<<1] == -1 && bintree[(p<<1)+1] == -1;
}

void add(ll x)
{
    int p=1;
    while(x)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        int tmp = x % 10;
        if(tmp&1)
        {
            p = rson;
        }
        else
        {
            p = lson;
        }

        if(bintree[p]==-1)
        {
            bintree[p] = 0;
        }

        if(tmp == x)
        {
            bintree[p]++;
        }
        x /= 10;
    }
}

int getit(char ss[])
{
    int res=0;
    int p = 1;
    int len = strlen(ss);
    int flag=0;
    for(int i=len-1;i>=0;i--)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        if(ss[i] == '1')
        {
            p = rson;
        }
        else
        {
            p = lson;
        }
        if(bintree[p] == -1 && ss[i] == '1') return 0;
        if(i == 0)
        {
            res += bintree[p];
           // flag = 1;
        }
    }
    while(p<maxn)
    {
        p <<= 1;
        if(bintree[p] == -1)
        {
            break;
        }
        res += bintree[p];
    }
    return res;
}

void del(ll x)
{
    int p=1;
    while(x)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        int tmp = x % 10;
        if(tmp&1)
        {
            p = rson;
        }
        else
        {
            p = lson;
        }

        if(bintree[p]==-1)
        {
            return;
        }

        if(tmp == x)
        {
            bintree[p]--;
        }
        x /= 10;
    }

    while(bintree[p]==0 && isleave(p))
    {
        bintree[p] = -1;
        p >>= 1;
    }
}

void work()
{
    init();
    int n;
    scanf("%d",&n);
    while(n--)
    {
        getchar();
        char c;
        scanf("%c",&c);
        if(c=='+')
        {
            ll x;
            scanf("%I64d",&x);
            add(x);
        }
        else if(c=='-')
        {
            ll x;
            scanf("%I64d",&x);
            del(x);
        }
        else
        {
            char s[22],ss[22];
            scanf("%s",s);
            int st=0;
            while(s[st] == '0' && s[st]) st++;
            int j=0;
            for(int i = st; s[i]; i++, j++)
                ss[j]=s[i];
            ss[j] = '\0';
          //  cout<<ss<<endl;
            int ans = getit(ss);
            printf("%d\n",ans >= 0 ? ans : 0);
        }
    }
}

int main()
{
#ifdef TT
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        printf("Case #%d: ",ca);
        work();
    }
#else
    work();
#endif // TT
}


#include <bits/stdc++.h>

//#define TT

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int maxn = 550000;
const int MOD = 1e9+7;

int bintree[maxn];

void init()
{
    memset(bintree,-1,sizeof(bintree));
}

bool isleave(int p)
{
    return bintree[p<<1] == -1 && bintree[(p<<1)+1] == -1;
}

void add(ll x)
{
    int p=1;
    while(x)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        int tmp = x % 10;
        if(tmp&1)
        {
            p = rson;
        }
        else
        {
            p = lson;
        }

        if(bintree[p]==-1)
        {
            bintree[p] = 0;
        }

        if(tmp == x)
        {
            bintree[p]++;
        }
        x /= 10;
    }
}

int getit(char ss[])
{
    int res=0;
    int p = 1;
    int len = strlen(ss);
    int flag=0;
    for(int i=len-1;i>=0;i--)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        if(ss[i] == '1')
        {
            p = rson;
        }
        else
        {
            p = lson;
        }
        if(bintree[p] == -1 && ss[i] == '1') return 0;
        if((isleave(p) || i == 0) && !flag)
        {
            res += bintree[p];
            flag = 1;
        }
    }
    while(p<maxn)
    {
        p <<= 1;
        if(bintree[p] == -1)
        {
            break;
        }
        res += bintree[p];
    }
    return res;
}

void del(ll x)
{
    int p=1;
    while(x)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        int tmp = x % 10;
        if(tmp&1)
        {
            p = rson;
        }
        else
        {
            p = lson;
        }

        if(bintree[p]==-1)
        {
            return;
        }

        if(tmp == x)
        {
            bintree[p]--;
        }
        x /= 10;
    }

    while(bintree[p]==0 && isleave(p))
    {
        bintree[p] = -1;
        p >>= 1;
    }
}

void work()
{
    init();
    int n;
    scanf("%d",&n);
    while(n--)
    {
        getchar();
        char c;
        scanf("%c",&c);
        if(c=='+')
        {
            ll x;
            scanf("%I64d",&x);
            add(x);
        }
        else if(c=='-')
        {
            ll x;
            scanf("%I64d",&x);
            del(x);
        }
        else
        {
            char s[22];
            scanf("%s",s);
            int ans = getit(s);
            printf("%d\n",ans >=0 ? ans : 0);
        }
    }
}

int main()
{
#ifdef TT
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        printf("Case #%d: ",ca);
        work();
    }
#else
    work();
#endif // TT
}


#include <bits/stdc++.h>

//#define TT

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int maxn = 10000000;
const int MOD = 1e9+7;

int bintree[maxn];

void init()
{
    memset(bintree,-1,sizeof(bintree));
}

bool isleave(int p)
{
    return bintree[p<<1] == -1 && bintree[(p<<1)+1] == -1;
}

void add(ll x)
{
    int p=1;
    while(x)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        int tmp = x % 10;
        if(tmp&1)
        {
            p = rson;
        }
        else
        {
            p = lson;
        }

        if(bintree[p]==-1)
        {
            bintree[p] = 0;
        }

        if(tmp == x)
        {
            bintree[p]++;
        }
        x /= 10;
    }
}

int getit(char ss[])
{
    int res=0;
    int p = 1;
    int len = strlen(ss);
    int flag=0;
    for(int i=len-1;i>=0;i--)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        if(ss[i] == '1')
        {
            p = rson;
        }
        else
        {
            p = lson;
        }
        if(bintree[p] == -1 && ss[i] == '1') return 0;
        if(i == 0)
        {
            res += bintree[p];
           // flag = 1;
        }
    }
    while(p<maxn)
    {
        p <<= 1;
        if(bintree[p] == -1)
        {
            break;
        }
        res += bintree[p];
    }
    return res;
}

void del(ll x)
{
    int p=1;
    while(x)
    {
        int lson = (p<<1);
        int rson = lson + 1;
        int tmp = x % 10;
        if(tmp&1)
        {
            p = rson;
        }
        else
        {
            p = lson;
        }

        if(bintree[p]==-1)
        {
            return;
        }

        if(tmp == x)
        {
            bintree[p]--;
        }
        x /= 10;
    }

    while(bintree[p]==0 && isleave(p))
    {
        bintree[p] = -1;
        p >>= 1;
    }
}

void work()
{
    init();
    int n;
    scanf("%d",&n);
    while(n--)
    {
        getchar();
        char c;
        scanf("%c",&c);
        if(c=='+')
        {
            ll x;
            scanf("%I64d",&x);
            add(x);
        }
        else if(c=='-')
        {
            ll x;
            scanf("%I64d",&x);
            del(x);
        }
        else
        {
            char s[22],ss[22];
            scanf("%s",s);
            int st=0;
            while(s[st] == '0' && s[st]) st++;
            int j=0;
            for(int i = st; s[i]; i++, j++)
                ss[j]=s[i];
            ss[j] = '\0';
          //  cout<<ss<<endl;
            int ans = getit(ss);
            printf("%d\n",ans >= 0 ? ans : 0);
        }
    }
}

int main()
{
#ifdef TT
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        printf("Case #%d: ",ca);
        work();
    }
#else
    work();
#endif // TT
}


#include<bits/stdc++.h>
using namespace std;

map<long long,long long> mmap;
map<long long,long long>::iterator it;
char in[1500];

bool isValid(long long k)
{
	int len = strlen(in);
	for(int a = len-1;a>=0;a--)
	{
		if((k%10)%2!=(in[a]-'0')) return false;
		k/=10;
	}
	while(k!=0)
	{
		if((k%10)%2!=0) return false;
		k/=10;
	}
	return true;
}

int main()
{
	int n;
	scanf("%d",&n); getchar();
	for(int a=0;a<n;a++)
	{
		char temp;
		scanf("%c",&temp); getchar();
		if(temp=='?')
		{
			scanf("%s",in); getchar();
			long long ans = 0;
			for(it = mmap.begin();it!=mmap.end();++it)
			{
				if(isValid(it->first)) ans+=it->second;
			}
			printf("%lld\n",ans);
		}
		else
		{
			long long in;
			scanf("%lld",&in); getchar();
			if(temp=='-') mmap[in]--;
			else mmap[in]++;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

int vec[500000];

long long conv(long long a)
{
	long long ans = 0;
	long long mul = 1;
	while(a!=0)
	{
		if(a%2==1) ans = ans + mul;
		mul*=2;
		a/=10;
	}
	return ans;
}

char in[150];

int main()
{
	int n;
	scanf("%d",&n); getchar();
	for(int a=0;a<n;a++)
	{
		char temp;
		scanf("%c",&temp); getchar();
		if(temp=='?')
		{
			scanf("%s",in); getchar();
			long long ans = 0;
			int len = strlen(in);
			for(int a = 0 ; a < len ; a++)
				ans = ans * 2 + (in[a]-'0');
			printf("%lld\n",vec[ans]);
		}
		else
		{
			long long in;
			scanf("%lld",&in); getchar();
			long long val = conv(in);
			if(temp=='-') vec[val]--;
			else vec[val]++;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

map<long long,long long> mmap;
map<long long,long long>::iterator it;
char in[1500];

bool isValid(long long k)
{
	int len = strlen(in);
	for(int a = len-1;a>=0;a--)
	{
		if((k%10)%2!=(in[a]-'0')) return false;
		k/=10;
	}
	return true;
}

int main()
{
	int n;
	scanf("%d",&n); getchar();
	for(int a=0;a<n;a++)
	{
		char temp;
		scanf("%c",&temp); getchar();
		if(temp=='?')
		{
			scanf("%s",in); getchar();
			int ans = 0;
			for(it = mmap.begin();it!=mmap.end();++it)
			{
				if(isValid(it->first)) ans+=it->second;
			}
			printf("%lld\n",ans);
		}
		else
		{
			long long in;
			scanf("%lld",&in); getchar();
			if(temp=='-') mmap[in]--;
			else mmap[in]++;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

map<long long,long long> mmap;
map<long long,long long>::iterator it;
char in[1500];

bool isValid(long long k)
{
	int len = strlen(in);
	for(int a = len-1;a>=0;a--)
	{
		if((k%10)%2!=(in[a]-'0')) return false;
		k/=10;
	}
	while(k!=0)
	{
		if((k%10)%2!=0) return false;
	}
	return true;
}

int main()
{
	int n;
	scanf("%d",&n); getchar();
	for(int a=0;a<n;a++)
	{
		char temp;
		scanf("%c",&temp); getchar();
		if(temp=='?')
		{
			scanf("%s",in); getchar();
			long long ans = 0;
			for(it = mmap.begin();it!=mmap.end();++it)
			{
				if(isValid(it->first)) ans+=it->second;
			}
			printf("%lld\n",ans);
		}
		else
		{
			long long in;
			scanf("%lld",&in); getchar();
			if(temp=='-') mmap[in]--;
			else mmap[in]++;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

long long vec[1000000];

long long conv(long long a)
{
	long long ans = 0;
	long long mul = 1;
	while(a!=0)
	{
		if(a%2==1) ans = ans + mul;
		mul*=2;
		a/=10;
	}
	return ans;
}

char in[150];

int main()
{
	int n;
	scanf("%d",&n); getchar();
	for(int a=0;a<n;a++)
	{
		char temp;
		scanf("%c",&temp); getchar();
		if(temp=='?')
		{
			scanf("%s",in); getchar();
			long long ans = 0;
			int len = strlen(in);
			for(int a = 0 ; a < len ; a++)
				ans = ans * 2 + (in[a]-'0');
			printf("%lld\n",vec[ans]);
		}
		else
		{
			long long in;
			scanf("%lld",&in); getchar();
			long long val = conv(in);
			if(temp=='-') vec[val]--;
			else vec[val]++;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

map<long long,long long> mmap;
map<long long,long long>::iterator it;
char in[1500];

bool isValid(long long k)
{
	int len = strlen(in);
	for(int a = len-1;a>=0;a--)
	{
		if((k%10)%2!=(in[a]-'0')) return false;
		k/=10;
	}
	return true;
}

int main()
{
	int n;
	scanf("%d",&n); getchar();
	for(int a=0;a<n;a++)
	{
		char temp;
		scanf("%c",&temp); getchar();
		if(temp=='?')
		{
			scanf("%s",in); getchar();
			long long ans = 0;
			for(it = mmap.begin();it!=mmap.end();++it)
			{
				if(isValid(it->first)) ans+=it->second;
			}
			printf("%lld\n",ans);
		}
		else
		{
			long long in;
			scanf("%lld",&in); getchar();
			if(temp=='-') mmap[in]--;
			else mmap[in]++;
		}
	}
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct trip
{
  struct trip *even;
  struct trip *odd;
  int num;
  trip():even(NULL),odd(NULL),num(0)
  { }
};
struct trip root;
struct trip aaa[270000];
int n;
int pos=0;
char str[20];
void add()
{

     struct trip *tmp =&root;
     int len = strlen(str);
     for(int i=len-1;i>=0;i--)
     {
             if(str[i]%2==0)
             {
                   if(tmp->even==NULL)
                   {
                       tmp->even = aaa+pos;
                       pos++;
                       tmp=tmp->even;
                   }
                   else
                   tmp=tmp->even;

             }
             else
             {
                   if(tmp->odd==NULL)
                   {
                       tmp->odd = aaa+pos;
                       pos++;
                       tmp=tmp->odd;
                   }
                   else
                   tmp=tmp->odd;
             }

     }
     tmp->num++;
     return;
}
void sub()
{
     struct trip *tmp =&root;
     int len = strlen(str);
     for(int i=len-1;i>=0;i--)
     {
             if(str[i]%2==0)
             {
                   if(tmp->even==NULL)
                   {
                       tmp->even = aaa+pos;
                       pos++;
                       tmp=tmp->even;
                   }
                   else
                   tmp=tmp->even;

             }
             else
             {
                   if(tmp->odd==NULL)
                   {
                       tmp->odd = aaa+pos;
                       pos++;
                       tmp=tmp->odd;
                   }
                   else
                   tmp=tmp->odd;
             }

     }
     tmp->num--;
     return;
}
int check()
{
    int len = strlen(str);
    struct trip *tmp=&root;
    int ss=len-1;
    int ans=0;
    for(int i=0;i<len;i++)
    if(str[i]=='1')
    {
    ss=i;
    break;
    }
    for(int i=len-1;i>=ss;i--)
    {
              if(str[i]=='0')
             {
                   if(tmp->even==NULL)
                   return 0;
                   tmp=tmp->even;

             }
             else
             {
                   if(tmp->odd==NULL)
                   return 0;
                   tmp=tmp->odd;
             }
    }
    ans+=tmp->num;
    for(int i=ss-1;i>=0;i--)
    {
            if(tmp->even==NULL)
            break;
            tmp=tmp->even;
            ans+=tmp->num;
    }
    while(tmp->even!=NULL)
    {
       tmp=tmp->even;
       ans+=tmp->num;
    }
    return ans;
}
int main()
{//freopen("input.txt","r",stdin);
    char m;
   // printf("cdsacdsacs\n");
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
         //   printf("cdsacdsacs\n");
    scanf("%c%c%s",&m,&m,str);
    //printf("%s\nddddd\n",str);
    if(m=='+')
    {
    add();
    }
    else if(m=='-')
    {
    sub();
    }
    else
    printf("%d\n",check());
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct trip
{
  struct trip *even;
  struct trip *odd;
  int num;
  trip():even(NULL),odd(NULL),num(0)
  { }
};
struct trip root;
struct trip aaa[270000];
int n;
int pos=0;
char str[20];
void add()
{
     struct trip *tmp =&root;
     int len = strlen(str);
     for(int i=len-1;i>=0;i--)
     {
             if(str[i]%2==0)
             {
                   if(tmp->even==NULL)
                   {
                       tmp->even = aaa+pos;
                       pos++;
                       tmp=tmp->even;
                   }
                   else
                   tmp=tmp->even;

             }
             else
             {
                   if(tmp->odd==NULL)
                   {
                       tmp->odd = aaa+pos;
                       pos++;
                       tmp=tmp->odd;
                   }
                   else
                   tmp=tmp->odd;
             }

     }
     tmp->num++;
     return;
}
void sub()
{
     struct trip *tmp =&root;
     int len = strlen(str);
     for(int i=len-1;i>=0;i--)
     {
             if(str[i]%2==0)
             {
                   if(tmp->even==NULL)
                   {
                       tmp->even = aaa+pos;
                       pos++;
                       tmp=tmp->even;
                   }
                   else
                   tmp=tmp->even;

             }
             else
             {
                   if(tmp->odd==NULL)
                   {
                       tmp->odd = aaa+pos;
                       pos++;
                       tmp=tmp->odd;
                   }
                   else
                   tmp=tmp->odd;
             }

     }
     tmp->num--;
     return;
}
int check()
{
    int len = strlen(str);
    struct trip *tmp=&root;
    int ss=len-1;
    int ans=0;
    for(int i=0;i<len;i++)
    if(str[i]=='1')
    {
    ss=i;
    break;
    }
    for(int i=len-1;i>=ss;i--)
    {
              if(str[i]=='0')
             {
                   if(tmp->even==NULL)
                   return 0;
                   tmp=tmp->even;

             }
             else
             {
                   if(tmp->odd==NULL)
                   return 0;
                   tmp=tmp->odd;
             }
    }
    ans+=tmp->num;
    for(int i=ss-1;i>=0;i--)
    {
            tmp=tmp->even;
            ans+=tmp->num;
    }
    while(tmp->even!=NULL)
    {
       tmp=tmp->even;
       ans+=tmp->num;
    }
    return ans;
}
int main()
{
    char m;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
    scanf("%c%c%s",&m,&m,str);
    if(m=='+')
    {
    add();
    }
    else if(m=='-')
    {
    sub();
    }
    else
    printf("%d\n",check());
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct trip
{
  struct trip *even;
  struct trip *odd;
  int num;
  trip():even(NULL),odd(NULL),num(0)
  { }
};
struct trip root;
struct trip aaa[270000];
int n;
int pos=0;
char str[20];
void add()
{
     struct trip *tmp =&root;
     int len = strlen(str);
     for(int i=len-1;i>=0;i--)
     {
             if(str[i]%2==0)
             {
                   if(tmp->even==NULL)
                   {
                       tmp->even = aaa+pos;
                       pos++;
                       tmp=tmp->even;
                   }
                   else
                   tmp=tmp->even;

             }
             else
             {
                   if(tmp->odd==NULL)
                   {
                       tmp->odd = aaa+pos;
                       pos++;
                       tmp=tmp->odd;
                   }
                   else
                   tmp=tmp->odd;
             }

     }
     tmp->num++;
     return;
}
void sub()
{
     struct trip *tmp =&root;
     int len = strlen(str);
     for(int i=len-1;i>=0;i--)
     {
             if(str[i]%2==0)
             {
                   if(tmp->even==NULL)
                   {
                       tmp->even = aaa+pos;
                       pos++;
                       tmp=tmp->even;
                   }
                   else
                   tmp=tmp->even;

             }
             else
             {
                   if(tmp->odd==NULL)
                   {
                       tmp->odd = aaa+pos;
                       pos++;
                       tmp=tmp->odd;
                   }
                   else
                   tmp=tmp->odd;
             }

     }
     tmp->num--;
     return;
}
int check()
{
    int len = strlen(str);
    struct trip *tmp=&root;
    int ss=len-1;
    int ans=0;
    for(int i=0;i<len;i++)
    if(str[i]=='1')
    {
    ss=i;
    break;
    }
    for(int i=len-1;i>=ss;i--)
    {
              if(str[i]=='0')
             {
                   if(tmp->even==NULL)
                   {
                       tmp->even = aaa+pos;
                       pos++;
                       tmp=tmp->even;
                   }
                   else
                   tmp=tmp->even;

             }
             else
             {
                   if(tmp->odd==NULL)
                   {
                       tmp->odd = aaa+pos;
                       pos++;
                       tmp=tmp->odd;
                   }
                   else
                   tmp=tmp->odd;
             }
    }
    ans+=tmp->num;
    for(int i=ss-1;i>=0;i--)
    {
            tmp=tmp->even;
            ans+=tmp->num;
    }
    while(tmp->even!=NULL)
    {
       tmp=tmp->even;
       ans+=tmp->num;
    }
    return ans;
}
int main()
{
    char m;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
    scanf("%c%c%s",&m,&m,str);
    if(m=='+')
    {
    add();
    }
    else if(m=='-')
    {
    sub();
    }
    else
    printf("%d\n",check());
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define pii pair<ll,ll>
#define x1 dlfjl
#define x2 dkfj
#define x3 dflgkg
#define y1 dskgj
#define y2 dskfj
#define y3 dfhgdh
#define SQ 320
#define PI 3.14159265359
#define MOD 1000000007
#define ROW 5
#define COL 30000
typedef int ll;
using namespace std;
ll n,i,j,k,z,m,q,h,r,timer,mx,l, cnt, u,v,t,s,tot,x,y;
ll a[200005];
map<string, int> f;
string g(string x) {
    string ans;
    for (int i = 0; i < 20-x.size(); i++) {
        ans.push_back('0');
    }
    for (int i = 0; i < x.size(); i++) {
        ans.push_back((x[i]-'0')%2+'0');
    }
    return ans;
}
int main() {
    freopen("input.txt","r",stdin);
    cin >> n;
    for (int i = 0; i < n; i++) {
        char c;
        string s;
        cin >> c >> s;
        if (c == '+')
            f[g(s)]++;
        else
        if (c == '-')
            f[g(s)]--;
        else
            cout << f[g(s)] << endl;
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
#include <deque>
#include <stack>
#include <stdio.h>
#include <map>
#include <set>
#include <time.h>
#include <string>
#include <fstream>
#include <queue>
#include <bitset>
#include <cstdlib>
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define pdd pair<double,double>
#define pii pair<ll,ll>
#define PI 3.14159265358979323846
#define MOD 1000000007
#define MOD2 1000000009
#define INF ((ll)1e+18)
#define x1 fldgjdflgjhrthrl
#define x2 fldgjdflgrtyrtyjl
#define y1 fldggfhfghjdflgjl
#define y2 ffgfldgjdflgjl
#define N 202100
typedef long long ll;
typedef long double ld;
using namespace std;
ll i,j,n,k,l,m,x,y,tot, flag,h,r,ans,koko;
ll a[1000500];
string s;
ll f(ll x)
{
	vector<ll> f;
	while (x)
	{
		f.push_back(x%2);
		x /= 10;
	}
	while (f.size() > 1 && f.back() == 0)
	{
		f.pop_back();
	}
	reverse(f.begin(), f.end());
	ll ans = 0;
	for (int i = 0; i < f.size(); i++)
		ans = ans*2 + f[i];
	return ans;
}
int main() {
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin >> n;
	for (i = 0; i < n; i++)
	{
		char c;
		cin >> c >> m;
		if (c == '+')
		   a[f(m)]++;
		else
		if (c == '-')
		   a[f(m)]--;
		else
			cout << a[f(m)] << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define pii pair<ll,ll>
#define x1 dlfjl
#define x2 dkfj
#define x3 dflgkg
#define y1 dskgj
#define y2 dskfj
#define y3 dfhgdh
#define SQ 320
#define PI 3.14159265359
#define MOD 1000000007
#define ROW 5
#define COL 30000
typedef int ll;
using namespace std;
ll n,i,j,k,z,m,q,h,r,timer,mx,l, cnt, u,v,t,s,tot,x,y;
ll a[200005];
map<string, int> f;
string g(string x) {
    string ans;
    for (int i = 0; i < 20-x.size(); i++) {
        ans.push_back('0');
    }
    for (int i = 0; i < x.size(); i++) {
        ans.push_back((x[i]-'0')%2+'0');
    }
    return ans;
}
int main() {
    //freopen("input.txt","r",stdin);
    cin >> n;
    for (int i = 0; i < n; i++) {
        char c;
        string s;
        cin >> c >> s;
        if (c == '+')
            f[g(s)]++;
        else
        if (c == '-')
            f[g(s)]--;
        else
            cout << f[g(s)] << endl;
    }
    return 0;
}