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

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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值