2-SAT

P4782 【模板】2-SAT
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
#include <bits/stdc++.h>
using namespace std;
namespace IO
{
#define siz 1 << 20
    char buf[siz], *pa = buf, *pb = buf;
#define gc()                                                              \
    (pa == pb && (pb = (pa = buf) + fread(buf, 1, siz, stdin), pa == pb)) \
        ? EOF                                                             \
        : *(pa++)
    inline int read()
    {
        int x = 0, f = 1;
        char ch;
        for (ch = gc(); !isdigit(ch); ch = gc())
            if (ch == '-') f = -1;
        for (; isdigit(ch); ch = gc())
        {
            x = (x << 3) + (x << 1) + (ch - '0');
        }
        return f * x;
    }
    inline void write(int x)
    {
        if (x < 0)
        {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
        return;
    }
#undef siz
#undef gc
} // namespace IO
using IO::read;
using IO::write;
#define truep(x) (x)
#define falsep(x) ((x) + n)
#define MAXN 1000010 << 1
#define MAXM 1000010 << 1
vector<int> e[MAXN];
stack<int> s;
queue<int> q;
int n, m, color[MAXN], idx, cnt, ind[MAXN], low[MAXN], dfn[MAXN];
bool book[MAXN];
void dfs(int u)
{
    dfn[u] = low[u] = ++idx;
    s.push(u), book[u] = true;
    for (int i = 0; i < e[u].size(); i++)
    {
        int v = e[u][i];
        if (!dfn[v])
        {
            dfs(v);
            low[u] = min(low[u], low[v]);
        }
        else if (book[v])
            low[u] = min(low[u], dfn[v]);
    }
    if (low[u] == dfn[u])
    {
        cnt++;
        int t;
        do
        {
            t = s.top();
            s.pop();
            color[t] = cnt;
            book[t] = false;
        } while (t != u);
    }
}
int main()
{
    n = read(), m = read();
    for (int i = 1; i <= m; i++)
    {
        int u = read(), a = read(), v = read(), b = read();
        e[truep(u) * a + falsep(u) * (a ^ 1)].push_back(truep(v) * (b ^ 1) +
                                                        falsep(v) * b);
        e[truep(v) * b + falsep(v) * (b ^ 1)].push_back(truep(u) * (a ^ 1) +
                                                        falsep(u) * a);
    }
    for (int i = 1; i <= (n << 1); i++)
        if (!dfn[i]) dfs(i);
    for (int i = 1; i <= n; i++)
    {
        if (color[truep(i)] == color[falsep(i)])
        {
            puts("IMPOSSIBLE");
            return 0;
        }
    }
    puts("POSSIBLE");
    for (int i = 1; i <= n; i++)
    {
        write(color[truep(i)] > color[falsep(i)]), putchar(' ');
    }
    return 0;
}