Title Link: http://poj.org/problem?id=3207
Description
Liympanda, one of Ikki ' s friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many times, he's tired of such easy games and wants to play another GAM E with Ikki.
Liympanda have a magic circle and he puts it on a plane, there is N points on its boundary in circular border:0, 1, 2, ..., n ? 1. Evil Panda claims that he is connecting m pairs of points. To connect points, Liympanda either places the link entirely inside the circle or entirely outside the circle. Now Liympanda tells Ikki no-links Touch inside/outside the circle, except on the boundary. He wants Ikki to figure out whether the is possible ...
Despaired at the minesweeping game just played, Ikki are 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 would be a line consisting of of of of the integers: n and m (n ≤1,000, m< /c3>≤500). The following m lines each contain integers ai and bi, which denote the endpoints of the ith wire. Every point would have the most one link.
Output
Output a line, either " panda is telling the truth...
" or " the evil panda is lying again
.
Sample Input
4 20 13 2
Sample Output
Panda is telling the truth ...
Source
POJ monthly--2007.03.04, Ikki
Test instructions
There is a circle on the plane, and the round edge is placed clockwise by 0. N-1 a total of n points. Now to the M-bar, such as a A, a to B can be connected from the inside of the circle, or from the outside of the circle. For your information, each point can have a maximum of one edge. Ask if you can connect this m-edge so that none of these edges intersect.
Ps:
For each link, either outside the circle, or within the circle, and not at the same time,
Can only take one, to determine whether the M-link is legal, that is, M-Link does not conflict,
This is the typical 2-sat problem. Consider each link I as a point, and if link is within a circle,
Then I is chosen, if it is outside the circle, I am chosen. For two lines (I,J), if i,j cannot simultaneously
In the circle, you can also launch both cannot be at the same time outside the circle, this proof is very easy, the reader can
To be self-certified. I, J can not be in the circle at the same time, there is an edge (I, J '), (J, I '), (i ', j), (J ', I)
(This is determined by the composition of the 2-sat, in particular, can be seen from the symmetry of the solution 2-sat problem.)
This paper). After the construction of the map, the question is to determine whether there is a solution to 2-sat problem,
First the strong connected component of the original image, and the contraction point, (Here we say: (I,i ') belong to the same group),
It is possible to determine whether the existence (I,i ') belongs to the same group, and if it exists, it is impossible if it does not exist.
The code is as follows:
< /span>
#include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm > #include <iostream>using namespace Std;//2-sat strong connected indent const int MAXN = 2010;//point twice times const int MAXM = 2000010;struct edge{int to; int next;} Edge[maxm];int head[maxn],tot;void init () {tot = 0; memset (head,-1,sizeof (Head));} void Addedge (int u,int v) {edge[tot].to = v; Edge[tot].next = Head[u]; Head[u] = tot++;} The value of the int low[maxn],dfn[maxn],stack[maxn],belong[maxn];//belong array 1~sccint index,top;int scc;bool Instack[MAXN];int num[ maxn];void Tarjan (int u)//tarjan to find strong connected component {int V; Low[u] = dfn[u] = ++index; stack[top++] = u; Instack[u] = true; for (int i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].to; if (! Dfn[v]) {Tarjan (v); if (Low[u] > Low[v]) low[u] = Low[v]; } else if (Instack[v] && low[u] > Dfn[v]) low[u] = Dfn[v]; } if (low[u] = = Dfn[u]) { scc++; do {v = stack[--top]; INSTACK[V] = false; BELONG[V] = SCC; } while (V! = u); }}//determine if there is a solution bool solvable (int n)//n is the total number, need to select half {memset (dfn,0,sizeof)); memset (instack,false,sizeof (instack)); Index = SCC = top = 0; for (int i = 1; I <= 2*n; i++) if (! Dfn[i]) Tarjan (i); for (int i = 1; I <= n; i++) if (belong[2*i] = = Belong[2*i+1]) return false; return true;} int A[MAXN], b[maxn];int N, m;void Build_grap () {for (int i = 1; I <= m; i++) {for (int j = i+1; j <= m; J + +) {if (a[i]<=a[j]&&b[i]>=a[j]&&b[i]<=b[j) | | (A[i]>=a[j]&&a[i]<=b[j]&&b[i]>=b[j])) {Addedge (2*i,2*j+1);//internal and external Addedge (2*j,2*i+1);//Inside and outside Addedge (2*I+1,2*J);//Outside Addedge (2*j+1,2*i);//Outer}}}}int main () {while (~scanf ("%d%d", &n, &m)) {if (n = = 0 && m = = 0) break; Init (); for (int i = 1; I <= m; i++) {scanf ("%d%d", &a[i],&b[i]); if (A[i] > B[i]) {int tt = A[i]; A[i] = B[i]; B[i] = TT; }} build_grap (); if (solvable (n))//Determine if there is a solution {printf ("Panda is telling the truth...\n"); } else {printf ("The Evil Panda is lying again\n"); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3207 Ikki's story Iv-panda ' s Trick (2-sat AH)