zoukankan      html  css  js  c++  java
  • bzoj1823满汉全席

    2-sat模板

    这篇博客写得非常好

    传送门

    //Achen
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<ctime>
    #include<cmath>
    const int N=1007; 
    typedef long long LL;
    using namespace std;
    int T,n,m,bel[N],dfn[N],low[N];
    
    template<typename T> void read(T &x) {
        char ch=getchar(); x=0; T f=1;
        while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
        if(ch=='-') f=-1,ch=getchar();
        for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    }
    
    int ecnt,fir[N],nxt[20007],to[20007];
    void add(int u,int v) {
        nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v;
    }
    
    int get() {
        int x; char o=getchar();
        while(o!='h'&&o!='m') o=getchar(); read(x);
        return o=='h'?(x<<1):(x<<1|1);
    }
    
    int que[N],dfs_clock,top,tot;
    void tarjan(int x) {
        dfn[x]=low[x]=++dfs_clock;
        que[++top]=x;
        for(int i=fir[x];i;i=nxt[i]) {
            if(!dfn[to[i]]) {
                tarjan(to[i]);
                low[x]=min(low[x],low[to[i]]);
            }
            else if(!bel[to[i]]) low[x]=min(low[x],dfn[to[i]]);
        }
        if(dfn[x]==low[x]) {
            tot++;
            while(top) {
                int tp=que[top--];            
                bel[tp]=tot;
                if(tp==x) break;
            }
        }
    }
    
    int main() {
    #ifdef DEBUG
        freopen(".in","r",stdin);
        freopen(".out","w",stdout);
    #endif
        read(T);
        while(T--) {
            read(n); read(m); ecnt=0;
            memset(dfn,0,sizeof(dfn));
            memset(bel,0,sizeof(bel));
            memset(fir,0,sizeof(fir));
            for(int i=1;i<=m;i++) {
                int x=get();
                int y=get();
                add(x^1,y); add(y^1,x);
            }
            dfs_clock=tot=0;
            for(int i=2;i<=(n<<1|1);i++) if(!dfn[i]) tarjan(i);
            int fl=0;
            for(int i=2;i<=(n<<1|1);i+=2) 
                if(bel[i]==bel[i|1]) {
                    fl=1; break;
                }
            if(fl) puts("BAD");
            else puts("GOOD"); 
        }
        return 0;
    }
    View Code
  • 相关阅读:
    异常问题处理记录(转载篇)
    linux服务器出现大量连接:sshd: root@notty
    第4章 Python运算符
    第2章 python基础知识
    第1章 python环境搭建
    Tomcat漏洞升级
    第3章 数据类型、运算符和表达式
    第2章 C语言基础知识
    第1章 概述
    第1章 企业管理概论
  • 原文地址:https://www.cnblogs.com/Achenchen/p/8127158.html
Copyright © 2011-2022 走看看