POJ 3207 Ikki's Story IV

Ikki's Story IV - Panda's Trick
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7296   Accepted: 2705

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, …, 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: n and m (n ≤ 1,000, m ≤ 500). The following m lines each contain two integers ai and bi, which denote the endpoints of the ith 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

4 2
0 1
3 2

Sample Output

panda is telling the truth...

Source

 
   2-sat ,点为线段。
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#pragma comment(linker, "/STACK:10240000000000,10240000000000")
using namespace std;
typedef long long LL ;
const int Max_N=5008 ;
const int Max_M=1000008 ;
int id ;
int vec[Max_N] ,mystack[Max_N] ,top;
int low[Max_N] ,dfn[Max_N] ,idx ,num ;
bool instack[Max_N] ;
int belong[Max_N] ;  //belong[i] ,i为哪个联通分量
//int sum[Max_N] ;  //缩点后每个联通分量中点的个数
struct Edge{
      int v ;
      int next ;
};
Edge edge[Max_M] ;
inline void add_edge(int u,int v){
    edge[id].v=v ;
    edge[id].next=vec[u] ;
    vec[u]=id++ ;
}
void tarjan(int u){
   low[u]=dfn[u]=idx++ ;
   mystack[++top]=u ;
   instack[u]=1 ;
   for(int e=vec[u];e!=-1;e=edge[e].next){
       int v=edge[e].v ;
       if(dfn[v]==-1){
          tarjan(v) ;
          low[u]=Min(low[u],low[v]) ;
       }
       else if(instack[v])
          low[u]=Min(low[u],dfn[v]) ;
   }
   if(low[u]==dfn[u]){
       int v ;
       num++ ;
       do{
          v=mystack[top--] ;
          instack[v]=0 ;
          belong[v]=num ;
         // sum[num]++ ;
       }while(v!=u) ;
   }
}
void init(){
    idx=1 ;
    top=-1 ;
    num=0 ;
    id=0;
    memset(dfn,-1,sizeof(dfn)) ;
    memset(vec,-1,sizeof(vec)) ;
    memset(instack,0,sizeof(instack)) ;
   // memset(sum,0,sizeof(sum)) ;
}
int N ;
int judge(){
   for(int i=1;i<=N;i++){
       if(belong[i]==belong[i+N])
            return 0 ;
   }
   return 1 ;
}
struct Line{
    int s ;
    int t ;
};
Line L[Max_N] ;
int cross(Line A ,Line B){
    if(B.s<A.s&&A.s<B.t&&B.t<A.t)
        return 1 ;
    if(A.s<B.s&&B.s<A.t&&A.t<B.t)
        return 1 ;
    return  0  ;
}
int main(){
   int  m ;
   while(scanf("%d%d",&m,&N)!=EOF){
        for(int i=1;i<=N;i++)
            scanf("%d%d",&L[i].s,&L[i].t) ;
        init()  ;
        for(int i=1;i<=N;i++)
           for(int j=i+1;j<=N;j++){
                if(cross(L[i],L[j])){
                      add_edge(i,j+N) ;
                      add_edge(j,i+N) ;
                      add_edge(i+N,j) ;
                      add_edge(j+N,i) ;
                }
        }
        for(int i=1;i<=2*N;i++){
            if(dfn[i]==-1)
                tarjan(i)  ;
        }
        if(judge())
            puts("panda is telling the truth...")  ;
        else
            puts("the evil panda is lying again")  ;
   }
   return 0 ;
}
原文地址:https://www.cnblogs.com/liyangtianmen/p/3389492.html