Skip to content

Commit ee5d83d

Browse files
committed
2-SAT可满足性问题(附POJ - 3678 Katu Puzzle代码)
1 parent 2643637 commit ee5d83d

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

2-Satisfiability.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <cstdio>
2+
3+
using namespace std;
4+
5+
struct vertex
6+
{
7+
int first, dfn, low, col;
8+
}V[2010];
9+
10+
struct edge
11+
{
12+
int endp, next;
13+
}E[4000000];
14+
15+
int ivc, iec, ec = 1, dfn = 1, col = 1, S[2010], sp = 0;
16+
bool ins[2010];
17+
18+
inline int min(int x, int y)
19+
{
20+
return x < y ? x : y;
21+
}
22+
23+
void add_edge(int u, int v)
24+
{
25+
E[ec].next = V[u].first;
26+
V[u].first = ec;
27+
E[ec].endp = v;
28+
ec++;
29+
}
30+
31+
void DFS(int u)
32+
{
33+
S[sp++] = u;
34+
ins[u] = true;
35+
V[u].dfn = V[u].low = dfn++;
36+
for (int cur = V[u].first; cur != 0; cur = E[cur].next)
37+
{
38+
if (V[E[cur].endp].dfn == 0)
39+
{
40+
DFS(E[cur].endp);
41+
}
42+
if (ins[E[cur].endp] == true && V[E[cur].endp].low < V[u].low)
43+
{
44+
V[u].low = V[E[cur].endp].low;
45+
}
46+
}
47+
if (V[u].low == V[u].dfn)
48+
{
49+
int temp;
50+
do
51+
{
52+
temp = S[--sp];
53+
ins[temp] = false;
54+
V[temp].col = col;
55+
} while (temp != u);
56+
col++;
57+
}
58+
}
59+
60+
int main()
61+
{
62+
int x, y, c;
63+
char op[10];
64+
scanf("%d%d", &ivc, &iec);
65+
for (int i = 0; i < iec; i++)
66+
{
67+
scanf("%d%d%d%s", &x, &y, &c, op);
68+
switch (op[0])
69+
{
70+
case 'A':
71+
if (c == 0)
72+
{
73+
add_edge(x, y + ivc);
74+
add_edge(y, x + ivc);
75+
}
76+
else
77+
{
78+
add_edge(x + ivc, x);
79+
add_edge(y + ivc, y);
80+
}
81+
break;
82+
case 'O':
83+
if (c == 0)
84+
{
85+
add_edge(x, x + ivc);
86+
add_edge(y, y + ivc);
87+
}
88+
else
89+
{
90+
add_edge(x + ivc, y);
91+
add_edge(y + ivc, x);
92+
}
93+
break;
94+
case 'X':
95+
if (c == 0)
96+
{
97+
add_edge(x, y);
98+
add_edge(y, x);
99+
add_edge(x + ivc, y + ivc);
100+
add_edge(y + ivc, x + ivc);
101+
}
102+
else
103+
{
104+
add_edge(x, y + ivc);
105+
add_edge(y, x + ivc);
106+
add_edge(x + ivc, y);
107+
add_edge(y + ivc, x);
108+
}
109+
break;
110+
}
111+
}
112+
for (int i = ivc * 2 - 1; i >= 0; i--)
113+
{
114+
if (V[i].dfn == 0)
115+
{
116+
DFS(i);
117+
}
118+
}
119+
for (int i = 0; i < ivc; i++)
120+
{
121+
if (V[i].col == V[i + ivc].col)
122+
{
123+
printf("NO");
124+
return 0;
125+
}
126+
}
127+
printf("YES");
128+
return 0;
129+
}

0 commit comments

Comments
 (0)