• hdu3062(2SAT入门)


    2-SAT入门题。类似和平委员会。

    模板敲错了,1W

    更改过来后,2Y

    View Code
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    #define E 1000500
    #define V 2005
    int top,cnt,index,n,m,ecnt;
    bool instack[V];
    int stack[V],id[V],dfn[V],low[V],in[V];
    int head[V];
    struct edge{
        int s,t,next;
    }e[E];
    void addedge(int u,int v){
        e[ecnt].s=u;
        e[ecnt].t=v;
        e[ecnt].next=head[u];
        head[u]=ecnt++;
    }
    void tarjan(int u){
        int v;
        int tmp;
        dfn[u]=low[u]=++index;
        instack[u]=true;
        stack[++top]=u;
        for(int k=head[u];k!=-1;k=e[k].next){
            v=e[k].t;
            if(!dfn[v]){
                tarjan(v);
                if(low[v]<low[u])
                    low[u]=low[v];
            }
            else if(instack[v] && dfn[v]<low[u]){
                low[u]=dfn[v];
            }
        }
        if(dfn[u]==low[u]){
            cnt++;
            do{
                tmp=stack[top--];
                instack[tmp]=false;
                id[tmp]=cnt;
            }
            while(tmp!=u);
        }
    }
    void build(){
        ecnt=0;
        memset(head,-1,sizeof(head));
        int a1,a2,c1,c2;
        while(m--){
            scanf("%d%d%d%d",&a1,&a2,&c1,&c2);
            if(c1 && c2){
                addedge(a1+n,a2);
                addedge(a2+n,a1);
            }
            if(!c1 && c2){
                addedge(a1,a2);
                addedge(a2+n,a1+n);
            }
            if(c1 && !c2){
                addedge(a1+n,a2+n);
                addedge(a2,a1);
            }
            if(!c1 && !c2){
                addedge(a1,a2+n);
                addedge(a2,a1+n);
            }
        }
    }
    void solve(){
        top=cnt=index=0;
        memset(dfn,0,sizeof(dfn));
        for(int i=0;i<2*n;i++)
            if(!dfn[i])
                tarjan(i);
    }
    void judge(){
        bool flag=0;
        for(int i=0;i<n;i++){
            if(id[i]==id[i+n]){
                flag=1;
                break;
            }
        }
        if(flag) printf("NO\n");
        else printf("YES\n");
    }
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            build();
            solve();
            judge();
        }
        return 0;
    }
  • 相关阅读:
    sql server 2016新特性 查询存储(Query Store)的性能影响
    Spring 事务管理详情介绍
    python爬虫之快速对js内容进行破解
    python爬虫的一个常见简单js反爬
    温习pycharm
    宋朝官员分析随堂理解笔记
    K-Means改进模式
    jupyter 饼图
    WebDriver常用的API使用详解
    从浏览器启动开始
  • 原文地址:https://www.cnblogs.com/markliu/p/2747050.html
Copyright © 2020-2023  润新知