POJ3207 Ikki's Story IV - Panda's Trick < 2-SAT >

Problem

Ikki’s Story IV - Panda’s Trick

Description

liympanda, one of Ikki’s friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many times, he is tired of such easy games and wants to play another game with Ikki.
liympanda has a magic circle and he puts it on a plane, there are n points on its boundary in circular border: 0,1,2,,n10, 1, 2, \cdots , n-1. Evil panda claims that he is connecting m pairs of points. To connect two points, liympanda either places the link entirely inside the circle or entirely outside the circle. Now liympanda tells Ikki no two links touch inside/outside the circle, except on the boundary. He wants Ikki to figure out whether this is possible…
Despaired at the minesweeping game just played, Ikki is totally at a loss, so he decides to write a program to help him.

Input

The input contains exactly one test case.
In the test case there will be a line consisting of of two integers: nn and mm (n1,000,m500)(n \le 1,000, m \le 500). The following mm lines each contain two integers aia_i and bib_i, which denote the endpoints of the ithi^{th} wire. Every point will have at most one link.

Output

Output a line, either “panda is telling the truth…” or “the evil panda is lying again”.

Sample Input

1
2
3
4 2
0 1
3 2

Sample Output

1
panda is telling the truth...

标签:2-SAT

Translation

nn个数排成一圈,给出若干条两点间的线段,可以在圈内连,也可在圈外连,问能否使得无相交线段(端点相交不算)

Solution

本题是2SAT\mathrm{2-SAT}的判定裸题。
把每条线段拆成两个点,即第ii条为iiii',分别代表ii在圈内和在圈外。若线段aabb相交,则连边aba\to b'aba'\to b,这样跑一个tarjantarjan再判断iiii'是否在一个联通块内即可判断是否有冲突。

Code

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
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#define MAX_M 500
using namespace std;
int n, m, x[MAX_M+5], y[MAX_M+5];
int dfn[MAX_M*2+5], low[MAX_M*2+5], id[MAX_M*2+5], index, cnt;
vector <int> G[MAX_M*2+5];
stack <int> sta;
bool insta[MAX_M*2+5];
void tarjan(int u) {
dfn[u] = low[u] = ++index;
sta.push(u), insta[u] = true;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
else if (insta[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
cnt++;
for (int i = sta.top(); ; i = sta.top()) {
id[i] = cnt;
sta.pop();
if (i == u) break;
}
}
}
void build() {
for (int i = 2; i <= m; i++)
for (int j = 1; j < i; j++)
if ((x[i] <= x[j] && y[i] >= x[j] && y[i] <= y[j]) || (x[i] >= x[j] && x[i] <= y[j] && y[i] >= y[j]))
G[i].push_back(j+m), G[j].push_back(i+m), G[i+m].push_back(j), G[j+m].push_back(i);
}
bool check() {
for (int i = 1; i <= m; i++)
if (id[i] == id[i+m])
return false;
return true;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x[i], &y[i]);
if (x[i] > y[i]) swap(x[i], y[i]);
}
build();
for (int i = 1; i <= m*2; i++)
if (!dfn[i])
tarjan(i);
if (check()) printf("panda is telling the truth...\n");
else printf("the evil panda is lying again\n");
return 0;
}