zoukankan      html  css  js  c++  java
  • 洛谷 P4782 【模板】2-SAT 问题

    传送门

    AcWing 2402 2-SAT 问题

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const double pi(acos(-1));
    const int inf(0x3f3f3f3f);
    const int mod(1e9 + 7);
    const int maxn(2e6 + 10);
    const int maxm(2e6 + 10);
    int ecnt, head[maxn];
    int tim, dfn[maxn], low[maxn];
    int scnt, id[maxn];
    bool vis[maxn];
    stack<int> st;
    
    struct edge {
        int to, nxt;
    } edges[maxm];
    
    template<typename T = int>
    inline const T read()
    {
        T 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 << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, char c = false)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (c) putchar(c);
    }
    
    void addEdge(int u, int v)
    {
        edges[ecnt].to = v;
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    void tarjan(int u)
    {
        dfn[u] = low[u] = ++tim;
        st.push(u);
        vis[u] = true;
        for (int i = head[u]; compl i; i = edges[i].nxt) {
            int v = edges[i].to;
            if (not dfn[v]) {
                tarjan(v);
                low[u] = min(low[u], low[v]);
            } else if (vis[v]) {
                low[u] = min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            int v = -1;
            ++scnt;
            do {
                v = st.top();
                st.pop();
                vis[v] = false;
                id[v] = scnt;
            } while (u not_eq v);
        }
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        int n = read(), m = read();
        memset(head, -1, sizeof head);
        while (m--) {
            int u = read() - 1, a = read(), v = read() - 1, b = read();
            addEdge(u * 2 + not a, v * 2 + b);
            addEdge(v * 2 + not b, u * 2 + a);
        }
        for (int i = 0; i < n * 2; ++i) {
            if (not dfn[i]) {
                tarjan(i);
            }
        }
        bool flag = false;
        for (int i = 0; i < n; ++i) {
            if (id[i * 2] == id[i * 2 + 1]) {
                flag = true;
                break;
            }
        }
        if (flag) {
            puts("IMPOSSIBLE");
        } else {
            puts("POSSIBLE");
            for (int i = 0; i < n; ++i) {
                if (id[i * 2] < id[i * 2 + 1]) {
                    write(0, ' ');
                } else {
                    write(1, ' ');
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    HTML/CSS基础知识(二)
    JS基础——变量
    HTML/CSS基础知识(四)
    NodeJS学习之win10安装与sublime配置
    HTML/CSS基础知识(一)
    HTML/CSS基础知识(三)
    win10安装git
    C#获取本机局域网IP和公网IP
    如何书写.md格式文档
    C# 获取硬盘空间信息 盘符总大小、剩余空间、已用空间
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13983453.html
Copyright © 2011-2022 走看看