从头T到尾(#`O′)
查看原帖
从头T到尾(#`O′)
227824
JK_LOVER楼主2020/8/9 21:02
#include<bits/stdc++.h>
using namespace std;
const int N = 2010;
int read(){
	int x = 0,f = 0;char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-')f=1;ch=getchar();}
	while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
	return f?-x:x;
}
struct Edge{int to,nxt;}e[N<<1];
int head[N],cnt = 1,st[N],n,m,top = 0,col[N],dfn[N],low[N],tot = 0,tim = 0;
void Init(){
	cnt = 1;tot = 0;top = 0;tim = 0;
	memset(col,0,sizeof(col));
	memset(dfn,0,sizeof(col));
	memset(low,0,sizeof(low));
	memset(st,0,sizeof(st));
}
void add(int x,int y){
	e[++cnt].nxt = head[x];e[cnt].to = y;head[x] = cnt;
}
void tarjin(int x){
	dfn[x] = low[x] = ++tim;st[++top] = x;
	for(int i = head[x];i;i = e[i].nxt){
		int y = e[i].to;
		if(!dfn[y]){
			tarjin(y);low[x] = min(low[x],low[y]);
		}
		else if(!col[y]) low[x] = min(low[x],dfn[y]);
	}
	if(dfn[x]==low[x]){
		col[x] = ++tot;
		while(st[top] != x){
			col[st[top]] = tot;
			top--;
		}
		top--;
	}
}
int main(){
//	ios::sync_with_stdio(0);
	int K;cin>>K;
	while(K--){
		Init();
		cin>>n>>m;
		for(int i = 1;i <= m;i++){
			char ch1[10],ch2[10];int a,b;
			scanf("%s%s",ch1,ch2);
			a = ch1[1] - '0';b = ch2[1] - '0';
			if(ch1[0] == 'm' && ch2[0] == 'h')
			{
				add(a+n,b+n);add(b,a);
			}
			if(ch1[0] == 'h' && ch2[0] == 'h')
			{
				add(a,b+n);add(b,a+n);
			}
			if(ch1[0] == 'm' && ch2[0] == 'm')
			{
				add(a+n,b);add(b+n,a);
			}
			if(ch1[0] == 'h' && ch2[0] == 'h')
			{
				add(a,b+n);add(b,a+n);
			}
		}
		for(int i = 1;i <= n*2;i++){
			if(!dfn[i]) tarjin(i);
		}
		int a = 0;
		for(int i = 1;i <= n;i++){
			if(col[i] == col[i+n] && !a){
				printf("BAD\n");a=1;
			}
		}
		if(!a) printf("GOOD\n");
	}
	return 0;
}

2020/8/9 21:02
加载中...