由于每个门只会被两个钥匙控制,那么两个钥匙的选或不选就能建立起一种对应关系。即如果门本来是开着的,那么用了一把必须用另一把,不用一把必须不用另一把;如果们本来是开着的,那么不用一把必须用另一把,用了一把必须不用另一把。

Tarjan 跑 2-SAT 随手切,注意 $n$ 和 $m$ 不要搞反。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// ==============================
// author: memset0
// website: https://memset0.cn
// ==============================
#include <bits/stdc++.h>
#define ll long long
#define rep(i,l,r) for (int i = l; i <= r; i++)
#define getc(x) getchar(x)
#define putc(x) putchar(x)

template <typename T> inline void read(T &x) {
x = 0; register char ch; register bool fl = 0;
while (ch = getc(), ch < 48 || 57 < ch) fl ^= ch == '-'; x = (ch & 15);
while (ch = getc(), 47 < ch && ch < 58) x = (x << 1) + (x << 3) + (ch & 15);
if (fl) x = -x;
}
template <typename T> inline void print(T x, char c = '\n') {
static int buf[40];
if (x == 0) { putc('0'); putc(c); return; }
if (x < 0) putc('-'), x = -x;
for (buf[0] = 0; x; x /= 10) buf[++buf[0]] = x % 10 + 48;
while (buf[0]) putc((char) buf[buf[0]--]);
putc(c);
}

const int maxn = 200010, maxm = 400010;

int n, m, t, x, cnt, pos, top;
int a[maxn], b[maxn][2], low[maxn], dfn[maxn], stk[maxn], ins[maxn], col[maxn];

int tot = 2, hed[maxn], nxt[maxm], to[maxm];

inline void add_edge(int u, int v) {
nxt[tot] = hed[u], to[tot] = v;
hed[u] = tot++;
}

void tarjan(int u) {
dfn[u] = low[u] = ++pos;
ins[u] = 1, stk[++top] = u;
for (int i = hed[u]; i; i = nxt[i]) {
int v = to[i];
if (!dfn[v]) {
tarjan(v);
low[u] = std::min(low[u], low[v]);
} else if (ins[v]) {
low[u] = std::min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
++cnt;
while (top) {
int v = stk[top--];
col[v] = cnt;
ins[v] = 0;
if (u == v) break;
}
}
}

int main() {

read(n), read(m);
for (int i = 1; i <= n; i++)
read(a[i]);
for (int i = 1; i <= m; i++) {
read(t);
for (int j = 1; j <= t; j++) {
read(x);
b[x][b[x][0] ? 1 : 0] = i;
}
}

for (int i = 1; i <= n; i++)
if (a[i]) {
add_edge(b[i][0], b[i][1]);
add_edge(b[i][1], b[i][0]);
add_edge(b[i][0] + m, b[i][1] + m);
add_edge(b[i][1] + m, b[i][0] + m);
} else {
add_edge(b[i][0], b[i][1] + m);
add_edge(b[i][1] + m, b[i][0]);
add_edge(b[i][1], b[i][0] + m);
add_edge(b[i][0] + m, b[i][1]);
}

for (int i = 1; i <= (m << 1); i++)
if (!dfn[i])
tarjan(i);

for (int i = 1; i <= (m << 1); i++)
if (col[i] == col[i + m]) {
puts("NO");
return 0;
}
puts("YES");

return 0;
}