模型:两个至少选一个。
#include<bits/stdc++.h>
#define ll long long
#define MP make_pair
#define P pair<long long, long long>
using namespace std;
const int N = 2e3 + 10;
int t, n, m, h[N], cnt;
int dfn[N], scc[N], ins[N], low[N], ind, col;
stack<int> s;
char l[3], r[3];
struct node {
int v, nt;
} no[N];
void add(int u, int v) {
no[cnt] = node{v, h[u]};
h[u] = cnt++;
}
void tarjan(int u) {
dfn[u] = low[u] = ++ind;
ins[u] = 1;
s.push(u);
for(int i = h[u]; ~i; i = no[i].nt) {
int v = no[i].v;
if(!dfn[v])
tarjan(v), low[u] = min(low[u], low[v]);
else if(ins[v])
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]) {
++col;
int v;
do {
v = s.top();
s.pop();
scc[v] = col;
ins[v] = 0;
} while(u != v);
}
}
int check() {
for(int i = 1; i <= n; i++)
if(scc[i] == scc[i + n])
return 0;
return 1;
}
int main() {
scanf("%d", &t);
while(t--) {
while(!s.empty())
s.pop();
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h), cnt = ind = col = 0;
memset(dfn, 0, sizeof dfn);
memset(ins, 0, sizeof ins);
for(int x, y, i = 1; i <= m; i++) {
scanf("%1s%d %1s%d", l, &x, r, &y);
if(l[0] == 'm' && r[0] == 'm')
add(x + n, y), add(y + n, x);
if(l[0] == 'h' && r[0] == 'h')
add(x, y + n), add(y, x + n);
if(l[0] == 'h' && r[0] == 'm')
add(x, y), add(y + n, x + n);
if(l[0] == 'm' && r[0] == 'h')
add(x + n, y + n), add(y, x);
}
for(int i = 1; i <= n * 2; i++)
if(!dfn[i])
tarjan(i);
printf(check() ? "GOOD\n" : "BAD\n");
}
return 0;
}