2-SAT poj3207将边看做点

Posted Billyshuai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2-SAT poj3207将边看做点相关的知识,希望对你有一定的参考价值。

Ikki‘s Story IV - Panda‘s Trick
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 10238   Accepted: 3762

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...

将边看成点,然后拆点,一个代表圆内边(以点代边),一个代表园外边.
由约束条件搞出哪些点在所建图中必定要相连,然后跑tajan得到联通块‘
并查集也是可以的

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1005;
const int M=2000005;
const int INF=1000000000;
int n,m,tot,head[N];
int dfn[N],low[N],index,instack[N],scc;
int top,st[N],fa[N];
int x[N],y[N];
struct edge{
   int v,nxt;
}e[M];
void init(){
   tot=index=scc=top=0;
   memset(dfn,0,sizeof(dfn));
   memset(instack,0,sizeof(instack));
   memset(head,-1,sizeof(head));
}
void insert(int x,int y){
   e[tot].v=y;
   e[tot].nxt=head[x];
   head[x]=tot++;
}
void Tarjan(int u){
   instack[u]=1;
   st[top++]=u;
   dfn[u]=low[u]=++index;
   for(int i=head[u];~i;i=e[i].nxt){
    int v=e[i].v;
    if(!dfn[v]) {
        Tarjan(v);
        low[u]=min(low[u],low[v]);
    }
    else if(instack[v]) low[u]=min(low[u],dfn[v]);
   }
   if(dfn[u]==low[u]){
    ++scc;int t;
    do{
        t=st[--top];
        instack[t]=0;
        fa[t]=scc;
    }while(t!=u);
   }
}
void build(){
   for(int i=1;i<=m;++i){
    scanf("%d%d",&x[i],&y[i]);
    ++x[i],++y[i];
    if(x[i]>y[i]) swap(x[i],y[i]);
   }
   for(int i=1;i<=m;++i) for(int j=i+1;j<=m;++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])){
    insert(i,j+m);
    insert(j,i+m);
    insert(i+m,j);
    insert(j+m,i);
   }
   n=2*m;
}
bool check(){
   for(int i=1;i<=m;++i) if(fa[i]==fa[i+m]) return false;
   return true;
}
void solve(){
   for(int i=1;i<=n;++i) if(!dfn[i]) Tarjan(i);
   if(check()) puts("panda is telling the truth...");
   else puts("the evil panda is lying again");
}
int main(){
    scanf("%d%d",&n,&m);
    init();
    build();
    solve();
}

 

以上是关于2-SAT poj3207将边看做点的主要内容,如果未能解决你的问题,请参考以下文章

poj 3207 Ikki's Story IV - Panda's Trick 2-sat

POJ 3207 Ikki&#39;s Story IV - Panda&#39;s Trick(2-sat)

POJ3207 Ikki's Story IV - Panda's Trick 2-sat

[2-SAT] poj 3207 Ikki&#39;s Story IV - Panda&#39;s Trick

poj 3207 Ikki's Story IV - Panda's Trick 2-SAT

poj 3207 Ikki's Story IV - Panda's Trick 2-SAT