【POJ - 3207】Ikki's Story IV - Panda's Trick(2-SAT之判断有解)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coldfresh/article/details/82987385

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…

**思路:**把边当做点,有两个域,分别是内存和外层,然后直接m^2建图,跑tarjan,判断是否合法。
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define maxn 1005
#define maxx 550005
using namespace std;
int head[maxn],to[maxx],_next[maxx],edge;
inline void addEdge(int x,int y)
{
    to[++edge]=y,_next[edge]=head[x],head[x]=edge;
}
struct node
{
    int a,b;
}p[505];
int dfn[maxn],low[maxn],_index=0;
int st[maxn],cnt=0;
bool inS[maxn];
int be[maxn],tot=0;
void tarjan(int u)
{
    dfn[u]=low[u]=++_index;
    st[++cnt]=u;inS[u]=true;
    for(int i=head[u];i;i=_next[i])
    {
        int v=to[i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(inS[v])low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        ++tot;int now;
        do
        {
            now=st[cnt--];
            inS[now]=false;
            be[now]=tot;
        }while(u!=now);
    }
}
int main()
{
    int n,m;
    cin>>n>>m;
    int x,y;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        if(x>y)swap(x,y);
        p[i].a=x;p[i].b=y;
    }
    for(int i=0;i<m;i++)
    for(int j=0;j<i;j++)
    {
        if(p[i].b<=p[j].a||p[j].b<=p[i].a)continue;
        if((p[i].a<=p[j].a&&p[j].b<=p[i].b)||((p[j].a<=p[i].a&&p[i].b<=p[j].b)))continue;
        addEdge(i<<1,j<<1|1);
        addEdge(j<<1|1,i<<1);
        addEdge(j<<1,i<<1|1);
        addEdge(i<<1|1,j<<1);
    }
    for(int i=0;i<2*m;i++) if(!dfn[i])tarjan(i);
    bool sign=true;
    for(int i=0;i<m;i++)
    {
        if(be[i<<1]==be[i<<1|1])
        {
            sign=false;
            break;
        }
    }
    if(sign)printf("panda is telling the truth...\n");
    else printf("the evil panda is lying again\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Coldfresh/article/details/82987385