zoukankan      html  css  js  c++  java
  • hdu3062 (2-SAT)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<map>
    #include<queue>
    #include<vector>
    #include<string>
    #include<fstream>
    using namespace std;
    #define rep(i, a, n) for(int i = a; i <= n; ++ i);
    #define per(i, a, n) for(int i = n; i >= a; -- i);
    typedef long long ll;
    const int N = 2e5 + 105;
    const int mod = 1e9 + 7;
    const double Pi = acos(- 1.0);
    const ll INF = 1e18;
    const int G = 3, Gi = 332748118;
    ll qpow(ll a, ll b) { ll res = 1; while(b){ if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1;} return res; }
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    // bool cmp(int a, int b){return a > b;}
    //
    
    int n, m;
    
    int head[N], cnt = 0;
    int low[N], dfn[N], Stack[N], Belong[N];
    int Index, top, scc;
    bool Instack[N];
    int to[N << 1], nxt[N << 1];
    
    void add(int u, int v){
        to[cnt] = v, nxt[cnt] = head[u], head[u] = cnt ++;
    }
    
    
    void Tarjan(int u){
        int v;
        low[u] = dfn[u] = ++ Index;
        Stack[top ++] = u;
        Instack[u] = true;
        for(int i = head[u]; i != -1; i = nxt[i]){
            v = to[i];
            if(!dfn[v]){
                Tarjan(v);
                if(low[u] > low[v] ) low[u] = low[v];
            }
            else if(Instack[v] && low[u] > dfn[v]) low[u] = dfn[v];
        }
        if(low[u] == dfn[u]){
            scc ++;
            do{
                v = Stack[-- top];
                Instack[v] = false;
                Belong[v] = scc;
            }while(v != u);
        }
    }
    
    
    void solve(){
        memset(dfn,0,sizeof(dfn));
        memset(Instack,false,sizeof(Instack));
        Index = top = scc = 0;
        for(int i = 1; i <= n * 2; ++ i)
            if(!dfn[i])
                Tarjan(i);
    }
    
    void init(){
        cnt = 0;
        memset(head,-1,sizeof(head));
    }
    
    int main()
    {
        while(~scanf("%d%d",&n,&m)){
            init();
            for(int i = 1; i <= m; ++ i){
                int x, tx, y, ty;
                scanf("%d%d%d%d",&x,&y,&tx,&ty);
                x ++ , y ++;
                add(x + n * tx, y + n * (ty ^ 1));
                add(y + n * ty, x + n * (tx ^ 1));
            }
            solve();
            int flag = 0;
            for(int i = 1; i <= n; ++ i){
                if(Belong[i] == Belong[i + n]){
                    flag = 1; break;
                }
            }
            if(flag) printf("NO
    ");
            else printf("YES
    ");
        }
        return 0;
    }
    
    
    
    
  • 相关阅读:
    全网最详细的Linux命令系列-ls命令
    Kubernetes 部署策略详解-转载学习
    Kubernetes工作流程--<1>
    详解CURL状态码,最全的代码列表
    Haproxy-4层和7层代理负载实战
    Keepalived+Nginx高可用实例
    实现FTP+PAM+MySQL环境,批量配置虚拟用户
    每秒处理10万订单乐视集团支付Mysql架构-转载
    构建 CDN 分发网络架构简析
    Linux系统入门简介<1>
  • 原文地址:https://www.cnblogs.com/A-sc/p/13629562.html
Copyright © 2011-2022 走看看