首页 > 代码库 > 2-SAT

2-SAT

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=200000+15;
 5 struct Edge {
 6     int x,y,next;
 7     Edge(int x=0,int y=0,int next=0):
 8         x(x),y(y),next(next) {}
 9 } edge[maxn];
10 int sumedge,head[maxn];
11 int n,m;
12 int ins(int x,int y) {
13     edge[++sumedge]=Edge(x,y,head[x]);
14     return head[x]=sumedge;
15 }
16 bool instack[maxn];
17 int top,Stack[maxn];
18 int dfn[maxn],low[maxn],tim;
19 bool vis[maxn];
20 int col[maxn],sumcol;
21 int dfs(int now) {
22     dfn[now]=low[now]=++tim;
23     Stack[++top]=now;
24     vis[now]=true;
25     instack[now]=true;
26     for (int u=head[now]; u; u=edge[u].next)
27         if (instack[edge[u].y])
28             low[now]=min(low[now],dfn[edge[u].y]);
29         else if (!vis[edge[u].y]) {
30             dfs(edge[u].y);
31             low[now]=min(low[now],low[edge[u].y]);
32         } else {
33         }
34     if (low[now]==dfn[now]) {
35         sumcol++;
36         col[now]=sumcol;
37         while (Stack[top]!=now) {
38             col[Stack[top]]=sumcol;
39             instack[Stack[top]]=false;
40             top--;
41         }
42         instack[now]=false;
43         top--;
44     }
45     return 0;
46 }
47 int main() {
48     scanf("%d%d",&n,&m);
49     for (int i=1; i<=m; i++) {
50         int x,y;
51         scanf("%d%d",&x,&y); //假设操作为x|y=1
52         ins(2*x-1,2*y);  //一个取0,推出另一个取1
53         ins(2*y-1,2*x);
54     }
55     for (int i=1; i<=2*n; i++)
56         if (!vis[i]) dfs(i);
57     for (int i=1; i<=n; i++)
58         if (col[2*i-1]==col[2*i]) {
59             printf("Wrong\n");
60             return 0;
61         }
62     return 0;
63 }

 

2-SAT