10分求助

P4171 [JSOI2010] 满汉全席

Takeda_Shingen @ 2019-12-28 08:01:51

#include<bits/stdc++.h>
using namespace std;
int dfn[100100],low[100010],head[100010],stk[100010];
int co[100010];
int S,cnt,top;
struct node{
    int from,to,next;
}e[100010];
void add(int from,int to){
    e[++cnt].next=head[from];
    e[cnt].to=to;
    head[from]=cnt;
}
void tarjan(int now){
    dfn[now]=low[now]=++cnt;
    stk[++top]=now;
    for(int i=head[now];i;i=e[i].next){
        int to=e[i].to;
        if(!dfn[to]){
            tarjan(to);
            low[now]=min(low[now],low[to]);
        }else if(!co[to]){
            low[now]=min(low[now],dfn[to]);
        }
    }
    if(dfn[now]==low[now]){
        co[now]=++S;
        while(now!=stk[top]){
            co[stk[top]]=S;
            top--;
        }
        top--;
    }
}
int T;
int n,m;
int main(){
    cin>>T;
    while(T--){
    top=0;cnt=0;S=0;
    memset(head,0,sizeof head);
    memset(co,0,sizeof co);
    memset(stk,0,sizeof stk);   
    memset(low,0,sizeof low);
    memset(e,0,sizeof e);
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        int X=0,Y=0,va,vb;
        char c1[5],c2[5];
        scanf("%s %s",c1,c2);
        int k;
        k=1; while(c1[k]>='0'&&c1[k]<='9')X=X*10+c1[k++]-'0';
        k=1; while(c2[k]>='0'&&c2[k]<='9')Y=Y*10+c2[k++]-'0';
        if(c1[0]=='m')va=0;
        if(c1[0]=='h')va=1;
        if(c2[0]=='m')vb=0;
        if(c2[0]=='h')vb=1;
        add(X+n*(1&va),Y+n*(1^vb));
        add(Y+n*(1&vb),X+n*(1^va));
    }
    for(int i=1;i<=n*2;i++){
        if(!dfn[i]){
            tarjan(i);
        }
    }
    int flag=1; 
    for(int i=1;i<=n;i++){
        if(co[i]==co[i+n]){
            printf("BAD\n");
            flag=0;
            break;
        }
    }
    if(flag==1)printf("GOOD\n");
/*  for(int i=1;i<=n;i++){
        if(co[i]<co[i+n]){
            printf("1 ");
        }else{
            printf("0 ");
        }
    }*/
    }
    return 0;
}

|