#include<bits/stdc++.h> using namespace std; const int N=1e3+7;
struct edge{ int to,next; }e[N<<1]; int last[N],cnt;
void add_edge(int x,int y){ cnt++; e[cnt].next=last[x]; e[cnt].to=y; last[x]=cnt; }
int n,m,x,y; char c1,c2;
int dfn[N],low[N],scc[N],dcnt,icnt; stack<int> st; void tarjan(int x){ st.push(x); dfn[x]=low[x]=++dcnt; for(int i=last[x];i;i=e[i].next){ int y=e[i].to; if(!dfn[y]){ tarjan(y); low[x]=min(low[x],low[y]); } else if(!scc[y]){ low[x]=min(low[x],dfn[y]); } } if(dfn[x]==low[x]){ icnt++; while(st.top()!=x){ scc[st.top()]=icnt; st.pop(); } scc[st.top()]=icnt; st.pop(); } }
int main(){ int k; scanf("%d\n",&k); while(k--){ scanf("%d %d\n",&n,&m); memset(last,0,sizeof(last));cnt=0; for(int i=1;i<=m;i++){ scanf("%c%d %c%d\n",&c1,&x,&c2,&y); if(c1=='m'&&c2=='m'){ add_edge(x+n,y);add_edge(y+n,x); } else if(c1=='m'&&c2=='h'){ add_edge(x+n,y+n);add_edge(y,x); } else if(c1=='h'&&c2=='m'){ add_edge(x,y);add_edge(y+n,x+n); } else{ add_edge(y,x+n);add_edge(x,y+n); } } memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(scc,0,sizeof(scc)); for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); int flag=0; for(int i=1;i<=n;i++) if(scc[i]==scc[i+n]) {flag=1;break;} if(!flag) printf("GOOD\n"); else printf("BAD\n"); } return 0; }
|