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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ std::cout<<x; } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=2e2+10,MAXM=1e5+10; int N,M; struct G { int next,to; }Edge[MAXM<<1]; int Head[MAXN],Total=1; inline void addEdge(int u,int v) { Edge[++Total]=(G){Head[u],v};Head[u]=Total; } int Dfn[MAXN],Low[MAXN],Stk[MAXN],Cnt,Top; bool Ins[MAXN]; int Scc[MAXN],Sizc[MAXN],Sc; void Tarjan(int x) { Dfn[x]=Low[x]=++Cnt,Ins[x]=1,Stk[++Top]=x; for(int e=Head[x],v;e;e=Edge[e].next) { if(!Dfn[(v=Edge[e].to)]) { Tarjan(v); checkMin(Low[x],Low[v]); } else if(Ins[v]) checkMin(Low[x],Dfn[v]); } if(Dfn[x]==Low[x]) { ++Sizc[++Sc]; while(Stk[Top]!=x) { Scc[Stk[Top]]=Sc,++Sizc[Sc]; Ins[Stk[Top]]=0,--Top; } Ins[x]=0,Scc[x]=Sc,--Top; } } int Test; inline void init() { Cnt=Sc=0; std::memset(Dfn,0,sizeof(Dfn)); std::memset(Scc,0,sizeof(Scc)); std::memset(Sizc,0,sizeof(Sizc)); std::memset(Head,0,sizeof(Head)),Total=1; } char as[10],bs[10]; struct Task { int u,i,v,j; inline void trans(char *a,char *b) { i=(a[0]=='h'),j=(b[0]=='h');u=v=0; int lena=strlen(a),lenb=strlen(b); for(int k=1;k<lena;++k) u=(u<<3)+(u<<1)+(a[k]^48); for(int k=1;k<lenb;++k) v=(v<<3)+(v<<1)+(b[k]^48); } inline void chk(){ write(u,' ',i,' ',v,' ',j,'\n'); } }; int main() {
read(Test); while(Test--) { read(N,M); init(); for(int i=1;i<=M;++i) { scanf("%s%s",as,bs); Task tmp;tmp.trans(as,bs); addEdge(tmp.u+(!tmp.i)*N,tmp.v+tmp.j*N), addEdge(tmp.v+(!tmp.j)*N,tmp.u+tmp.i*N); } for(int x=1;x<=(N<<1);++x) if(!Dfn[x]) Tarjan(x); bool scd=0; for(int x=1;x<=N;++x) if(Scc[x]==Scc[x+N]) { scd=1;puts("BAD"); break; } if(!scd) puts("GOOD"); } return 0; }
|