POJ 3207 Ikki's Story IV - Panda's Trick (2-sat,4级)

C - Ikki's Story IV - Panda's Trick
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u
Appoint description:

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...
思路:相交建边,相交不能全选,所以 x,y相交那么x->!y  y->!x; so !x or !y   

#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=2010;
class Edge
{
  public:int v,next;
};
class TWO_SAT
{
public:
  int dfn[mm],e_to[mm],stack[mm];
  Edge e[mm*mm];
  int edge,head[mm],top,dfs_clock,bcc;
  void clear()
  {
    edge=0;clr(head,-1);
  }
  void add(int u,int v)
  {
    e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
  }
  void add_my(int x,int xval,int y,int yval)
  {
    x=x+x+xval;y=y+y+yval;
    add(x,y);
  }
  void add_clause(int x,int xval,int y,int yval)
  {///x or y
    x=x+x+xval;
    y=y+y+yval;
    add(x^1,y);add(y^1,x);
  }
  int tarjan(int u)
  {
    int lowu,lowv;
    lowu=dfn[u]=++dfs_clock;
    int v; stack[top++]=u;
    for(int i=head[u];~i;i=e[i].next)
    {
      v=e[i].v;
      if(!dfn[v])
      {
        lowv=tarjan(v);
        lowu=min(lowv,lowu);
      }
      else if(e_to[v]==-1)//in stack
        lowu=min(lowu,dfn[v]);
    }
    if(dfn[u]==lowu)
    {
      ++bcc;
      do{
        v=stack[--top];
        e_to[v]=bcc;
      }while(v!=u);
    }
    return lowu;
  }
  bool find_bcc(int n)
  { clr(e_to,-1);
    clr(dfn,0);
    bcc=dfs_clock=top=0;
    FOR(i,0,2*n-1)
    if(!dfn[i])
      tarjan(i);
    for(int i=0;i<2*n;i+=2)
      if(e_to[i]==e_to[i^1])return 0;
    return 1;
  }
}two;
Edge f[mm];
int n,m;
bool intersect(int i,int j)
{ //int nw,nx,ny;
  if(f[i].v>f[i].next)swap(f[i].v,f[i].next);
  if(f[j].v>f[j].next)swap(f[j].v,f[j].next);
  if(f[i].v<f[j].v&&f[i].next>f[j].v&&f[i].next<f[j].next)return 1;
  if(f[j].v<f[i].v&&f[j].next>f[i].v&&f[j].next<f[i].next)return 1;
  return 0;
}
int main()
{ int a,b,c,d;
  while(~scanf("%d%d",&n,&m))
  {
    two.clear();
    FOR(i,1,m)
    {
      scanf("%d%d",&f[i].v,&f[i].next);
    }
    FOR(i,1,m)
    FOR(j,i+1,m)
    if(intersect(i,j))
    { //cout<<i<<" "<<j<<endl;
      two.add_clause(i-1,0,j-1,0);
      two.add_clause(i-1,1,j-1,1);
    }
    if(two.find_bcc(m*2))printf("panda is telling the truth...\n");
    else printf("the evil panda is lying again\n");
  }
  return 0;
}


posted @ 2013-09-19 20:53  剑不飞  阅读(83)  评论(0编辑  收藏  举报