• 【模板】2-SAT


    P4782 【模板】2-SAT 问题

    我...忘了输出POSSIBLE

    总是会忘else if(inst[v]&&dfn[v]<low[u])是else if不是if!!!

    然后tarjan要记得入栈...

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<stack>
    #include<algorithm> 
    using namespace std;
    #define Min(x,y) ((x)<(y)?(x):(y))
    const int N=1e6+5,M=1e6+5,inf=0x3f3f3f3f,P=9999973;
    int n,m;
    template <class t>void rd(t &x){
        x=0;int w=0;char ch=0;
        while(!isdigit(ch)) w|=ch=='-',ch=getchar();
        while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
        x=w?-x:x;
    }
    
    int head[N],tot=0;
    struct edge{int u,v,nxt;}e[M<<1];
    void add(int u,int v){
        e[++tot]=(edge){u,v,head[u]},head[u]=tot;
    }
    
    stack<int>s;
    bool inst[N];
    int idx=0,Bcnt=0,dfn[N],low[N],bl[N];
    void tarjan(int u){
        dfn[u]=low[u]=++idx;
        s.push(u),inst[u]=1;
        for(int i=head[u],v;i;i=e[i].nxt){
            v=e[i].v;
            if(!dfn[v]) tarjan(v),low[u]=Min(low[u],low[v]);
            else if(inst[v]&&dfn[v]<low[u]) low[u]=dfn[v];
        }
        if(dfn[u]==low[u]){
            int v;
            ++Bcnt;
            do{
                v=s.top(),s.pop();
                inst[v]=0,bl[v]=Bcnt;
            }while(u!=v);
        }
    }
    
    int main(){
    //    freopen("in.txt","r",stdin);
        rd(n),rd(m);
        for(int i=1,x,xx,y,yy;i<=m;++i){
            rd(x),rd(xx),rd(y),rd(yy);
            x=(x<<1)+xx,y=(y<<1)+yy;
            add(x,y^1),add(y,x^1);
        }
        for(int i=2;i<=(n<<1|1);++i)
        if(!dfn[i]) tarjan(i);
        for(int i=1;i<=n;++i)
        if(bl[i<<1]==bl[i<<1|1])
        {puts("IMPOSSIBLE");return 0;}
        puts("POSSIBLE");
        for(int i=1;i<=n;++i)
        printf("%d ",(bl[i<<1]<bl[i<<1|1]));
        return 0;
    }
    luoguP4782

    HDU - 3062 

    2-SAT模板题

    犯了1mol低级错误就不说了QAQ

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<stack>
    #include<algorithm> 
    using namespace std;
    const int N=2000+5,M=1e6+5,inf=0x3f3f3f3f,P=9999973;
    int n,m;
    template <class t>void rd(t &x){
        x=0;int w=0;char ch=0;
        while(!isdigit(ch)) w|=ch=='-',ch=getchar();
        while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
        x=w?-x:x;
    }
    
    int head[N],tot=0;
    struct edge{int u,v,nxt;}e[M<<1];
    void add(int u,int v){
        e[++tot]=(edge){u,v,head[u]},head[u]=tot;
    }
    
    stack<int>s;
    bool inst[N];
    int idx=0,Bcnt=0,dfn[N],low[N],bl[N];
    void tarjan(int u){
        dfn[u]=low[u]=++idx;
        s.push(u);inst[u]=1;
        for(int i=head[u],v;i;i=e[i].nxt){
            v=e[i].v;
            if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
            else if(inst[v]&&dfn[v]<low[u]) low[u]=dfn[v];
        }
        if(dfn[u]==low[u]){
            int v;++Bcnt;
            do{
                v=s.top();s.pop();
                bl[v]=Bcnt;
                inst[v]=0;
            }while(v!=u);
        }
    }
    
    int main(){
    //    freopen("in.txt","r",stdin);
        while(scanf("%d%d",&n,&m)!=EOF){
            memset(head,0,sizeof(head));
            tot=Bcnt=idx=0;
            memset(dfn,0,sizeof(dfn));
            memset(low,0,sizeof(low)); 
            memset(bl,0,sizeof(bl));
            memset(inst,0,sizeof(inst));
            while(!s.empty()) s.pop();
            for(int i=1,x,y,xx,yy;i<=m;++i){
                rd(x),rd(y),rd(xx),rd(yy);
                x=(x<<1)+xx,y=(y<<1)+yy;
                add(x,y^1),add(y,x^1);
            }
            for(int i=0;i<(n<<1);++i)
            if(!dfn[i]) tarjan(i);
            int flag=0;
            for(int i=0;i<n;++i)
            if(bl[i<<1]==bl[i<<1|1]) flag=1;
            if(!flag) puts("YES");
            else puts("NO");
        }
        return 0;
    }
  • 相关阅读:
    SystemManage_系统管理
    安装openoffice.org
    Skill_技巧
    squid代理服务器架设与维护
    FreeBSD 添加硬盘并分区操作说明
    常见游戏端口
    /usr was not properly dismounted 解决办法
    自动获取电信/网通等IP列表
    Cacti文档下载(linux/windows Cacti安装、cacti模板、cacti插件)
    实现基于DNS的负载均衡
  • 原文地址:https://www.cnblogs.com/lxyyyy/p/11284726.html
Copyright © 2020-2023  润新知