15129718wang @ 2021-05-24 18:32:45
#include<bits/stdc++.h>
#define long long int;
using namespace std;
const int N=1e5;
int n,m,k;
int u,v;
string c1,c2;
vector<int>a[N];
int low[N],dfn[N],ins[N],shuyu[N];
stack<int>s;
int tot,cnt;
int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;
c=getchar();
}
while(c<='9'&&c>='0'){
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
return f*x;
}
void add(int x,int y){
a[x].push_back(y);
}
int shuzi1(int l){
int ans=0;
for(int i=1;i<l;i++){
if(c1[i]<='9'&&c1[i]>='0')ans=(ans<<1)+(ans<<3)+c1[i]-'0';
}
return ans;
}
int shuzi2(int l){
int ans=0;
for(int i=1;i<l;i++){
if(c2[i]<='9'&&c2[i]>='0')ans=(ans<<1)+(ans<<3)+c2[i]-'0';
}
return ans;
}
void tajian(int x){
tot++;
dfn[x]=tot;
low[x]=tot;
s.push(x);
ins[x]=1;
for(int i=0;i<a[x].size();i++){
int y=a[x][i];
if(!dfn[y]){
tajian(y);
low[x]=min(low[x],low[y]);
}
else if(ins[i]){
low[x]=min(low[x],dfn[y]);
}
}
if(dfn[x]==low[x]){
int y;
cnt++;
do{
y=s.top();
s.pop();
ins[y]=0;
shuyu[y]=cnt;
}while(!s.empty()&&x!=y);
}
}
signed main(){
k=read();
while(k--){
tot=0;
cnt=0;
for(int i=0;i<=N;i++){
a[i].clear();
}
memset(low,0,sizeof low);
memset(dfn,0,sizeof dfn);
memset(ins,0,sizeof ins);
memset(shuyu,0,sizeof shuyu);
while(!s.empty()){
s.pop();
}
n=read();m=read();
for(int i=1;i<=m;i++){
cin>>c1;
cin>>c2;
int l1=c1.length();
int l2=c2.length();
int x=shuzi1(l1);
int y=shuzi2(l2);
if(c1[0]=='m'){
if(c2[0]=='m'){
add(x+n,y);
add(y+n,x);
}
else{
add(x+n,y+n);
add(y,x);
}
}
if(c1[0]=='h'){
if(c2[0]=='h'){
add(x,y+n);
add(y,x+n);
}
else{
add(x,y);
add(y+n,x+n);
}
}
}
for(int i=1;i<=2*n;i++){
if(!dfn[i])tajian(i);
}
int f=0;
for(int i=1;i<=n;i++){
if(shuyu[i]==shuyu[i+n]){
cout<<"BAD"<<endl;
f=1;
break;
}
}
if(f==0)cout<<"GOOD"<<endl;
}
return 0;
}
by 15129718wang @ 2021-05-24 19:25:01
else if(ins[i]){ low[x]=min(low[x],dfn[y]); }
ins[y]///2333333