60分求助
查看原帖
60分求助
359270
是青白呀白鸽子楼主2021/12/9 17:12

#4#6#7#9WA

#include<bits/stdc++.h>
using namespace std;
int n,m,k;
struct edge{
	int to,next;
}e[2005];
int first[205],oth[205],np=0,nownum=0;
bool vis[205];
stack<int>stk;

void add(int x,int y){
	e[++np]=(edge){y,first[x]};
	first[x]=np;
}

bool dfs(int i){
	if(vis[i])return 1;
	if(vis[oth[i]])return 0;
	stk.push(i);
	vis[i]=1;
	nownum++;
	for(int p=first[i];p;p=e[p].next)
	    if(!dfs(e[p].to))return 0;
	return 1;
}

int main(){
	scanf("%d",&k);
	while(k){
		bool ok=1;
		memset(first,0,sizeof(first));
		memset(oth,0,sizeof(oth));
		while(!stk.empty())stk.pop();
		np=0;
		nownum=0;
		memset(vis,0,sizeof(vis));
		k--;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
	        oth[i]=i+n;
	    for(int i=n+1;i<=2*n;i++)
	        oth[i]=i-n;
	    getchar();
		for(int i=1;i<=m;i++){
			char s[6];
			gets(s);
			int a,b;
			a=s[1]-'0';
			b=s[4]-'0';
			if(s[0]=='m'&&s[3]=='m'){
				add(a+n,b);
				add(b+n,a);
			}
			if(s[0]=='m'&&s[3]=='h'){
				add(a+n,b+n);
				add(b,a);
			}
			if(s[0]=='h'&&s[3]=='m'){
				add(a,b);
				add(b+n,a+n);
			}
			if(s[0]=='h'&&s[3]=='h'){
				add(a,b+n);
				add(b,a+n);
			}
		}
		
		for(int i=1;i<=n;i++){
			if(vis[i]||vis[oth[i]])continue;
			nownum=0;
			if(!dfs(i)){
				for(int j=1;j<=nownum;j++){
				    int x;
			    	x=stk.top();
			    	stk.pop();
			    	vis[x]=0;
		    	}
		    	nownum=0;
		    	if(!dfs(oth[i])){
		    		printf("BAD");
		    		ok=0;
		    		break;
		    	}
			}
		}
		if(ok)printf("GOOD");
		printf("\n");
	}
	return 0;
}
2021/12/9 17:12
加载中...