【BZOJ】1823: [JSOI2010]满汉全席(2-sat)

题目

传送门:QWQ

 

 

分析

 2-sat模板(然而辣鸡如我还是调了好久)

 

 

代码

//bzoj 1823 2-sat
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int get()
{
    int x; char c=getchar();
    while(c!='m'&&c!='h')c=getchar();
    if(c=='m')x=read()*2; else x=read()*2-1;
    return x;
}

struct Edge{
    int from,to;
};
vector<int> G[20005];
int low[4005], dfn[4005], sccno[4005], dfs_clock, scc_num;
stack<int> s;
void insert(int u,int v){
    G[u].push_back(v);
}
void tarjan(int u)
{
    dfn[u]=low[u]=++dfs_clock; s.push(u);
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u], low[v]);
        }else if(!sccno[v]){
            low[u]=min(low[u], dfn[v]);
        }
    }
    if(low[u]==dfn[u]){
        scc_num++;
        for(;;){
            int x=s.top(); s.pop(); 
            sccno[x]=scc_num;
            if(x==u) break;
        }
    }
}

int main()
{
    int n,m,T=read();
    while(T--)
    {
        while(!s.empty()) s.pop();
        n=read(); m=read();
        int x,y,xp,yp;
        for(int i=0;i<m;i++)
        {
            x=get(); y=get();
            if(x%2==0)xp=x--;
            else xp=x++;
            if(y%2==0)yp=y--;
            else yp=y++;
            insert(xp,y); insert(yp,x);
        }
        
        memset(dfn,0,sizeof(dfn)); memset(sccno,0,sizeof(sccno));
        dfs_clock=scc_num=0;
        for(int i=1;i<=2*n;i++)
            if(!dfn[i]) tarjan(i);
        
        bool flag=1;
        for(int i=1;i<=n;i++)
            if(sccno[2*i]==sccno[2*i-1]) flag=false;
        if(flag) puts("GOOD");
        else puts("BAD");
        for(int i=0;i<=2*n;i++) G[i].clear();
    }
    return 0;
}

 

 

 

posted @ 2018-03-12 22:07  noble_(noblex)  阅读(179)  评论(0编辑  收藏  举报
/* */