poj 3207(2-SAT)

poj 3207
1、注意加边
2、判两条线段是否重叠的方法

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
104
105
106
107
108
109
110
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#define ll long long
#define db double
#define ms(i,j) memset(i,j)
#define INF 100000000
#define llINF 1LL<<60
using namespace std;
const int MAXM = 500 + 5;
struct TwoSAT
{
int mark[MAXM*2];
int S[MAXM*2];
vector<int> G[MAXM*2];
int n, c;
void init(int n)
{
this->n = n;
for (int i=0;i<=2*n;i++)
{
G[i].clear();
mark[i] = false;
}
}
void addE(int x, int xv, int y, int yv)
{
x = x * 2 + xv;
y = y * 2 + yv;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
bool dfs(int x)
{
if (mark[x^1]) return false;
if (mark[x]) return true;
mark[x] = true;
S[++c] = x;
for (int i=0;i<G[x].size();i++)
{
if (!dfs(G[x][i])) return false;
}
return true;
}
bool solve()
{
for (int i=0;i<2*n;i+=2)
if (!mark[i]&&!mark[i+1])
{
c = 0;
if (!dfs(i))
{
while (c>0) mark[S[c--]] = false;
if (!dfs(i+1)) return false;
}
}
return true;
}
}ts;
struct linkz
{
int a, b;
}lk[MAXM];
int n, m;
int init()
{
for (int i=1;i<=m;i++)
{
scanf("%d%d", &lk[i].a, &lk[i].b);
if (lk[i].a>lk[i].b)
{
int t = lk[i].a;
lk[i].a = lk[i].b, lk[i].b = t;
}
}
ts.init(n);
for (int i=1;i<=m;i++)
for (int j=i+1;j<=m;j++)
{
int &x1 = lk[i].a, &y1 = lk[i].b, &x2 = lk[j].a, &y2 = lk[j].b;
if ( (x1<=x2 && y1<=y2 && x2<=y1) || (x1>=x2 && y1>=y2 && y2>=x1) )
{
ts.addE(i-1, 0, j-1, 0);
ts.addE(i-1, 1, j-1, 1);
}
}
}
int solve()
{
if (ts.solve())
{
printf("panda is telling the truth...\n");
} else printf("the evil panda is lying again\n");
}
int main()
{
while (scanf("%d%d", &n, &m)==2)
{
init();
solve();
}
return 0;
}

------ 本文结束 ------