[poj3207]Ikki's Story IV - Panda's Trick

3 篇文章 0 订阅

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…

题目大意

给你一个环,环上有n个点,m条边(每个点最多连一条边)问这m条边是否能不相交。

思路

这题是个阅读理解题。。。

一开始看不出来,但是一画图,就恍然大悟:

比如1 5连边,2,6连边,由于点是顺序排列的,一画图就可以发现,这两条边必须一个从圆外面连,一个从内部连,否则就会相交。如果再加入3 7这条边,那么就必须相交了。

so,这可以转化为2-SAT标准问题。

1:每个边看成2个点:分别表示在内部连接和在外部连接,只能选择一个。计作点i和点i’
2:如果两条边i和j必须一个画在内部,一个画在外部(一个简单判断就可以)
那么连边:
i->j’, 表示i画内部的话,j只能画外部,即j’
j->i’,同理
i’->j,同理
j’->i,同理
然后就是2-sat算法了,tarjan一下,如果有i和i’同属于一个强联通,返回false,否则就成立。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=1077*5;
stack<int> s;
int a[maxn][maxn],dfn[maxn],low[maxn],list[maxn],b[maxn];
bool ins[maxn];
int n,m,cnt=0,ss=0,sig=0;
struct E
{
    int to,next;
}e[maxn];
void add(int u,int v)
{
    e[++cnt].to=v; e[cnt].next=list[u]; list[u]=cnt;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++ss;
    s.push(u); ins[u]=1;
    for(int i=list[u]; i; i=e[i].next)
    {
        if(!dfn[e[i].to])
        {
            tarjan(e[i].to);
            if(low[u]>low[e[i].to]) low[u]=low[e[i].to];
        }else
        if(ins[e[i].to]&&low[u]>dfn[e[i].to]) low[u]=dfn[e[i].to];
    }
    if(dfn[u]==low[u])
    {
        int t;
        sig++;
//      printf("sig=%d\n",sig);
        do
        {
            t=s.top(); ins[t]=0;s.pop();
            b[t]=sig;
//          printf("t=%d\n",t);
        }while(u!=t);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d",&a[i][0],&a[i][1]);
        if(a[i][0]>a[i][1]) swap(a[i][0],a[i][1]);
    }
    for(int i=1; i<=m-1; i++) for(int j=i+1; j<=m; j++)
    {
        if(a[i][0]<a[j][0]&&a[i][1]>a[j][0]&&a[i][1]<a[j][1]||
        a[j][0]<a[i][0]&&a[i][0]<a[j][1]&&a[j][1]<a[i][1])
        {
            add(i,j+m); add(i+m,j); add(j,i+m); add(j+m,i);
//          printf("***\n");
        }
    }
    memset(ins,0,sizeof(ins));
    memset(dfn,0,sizeof(dfn));
    for(int i=1; i<=2*m; i++) if(!dfn[i]) tarjan(i);
//  for(int i=1; i<=m*2; i++) printf("%d\n",b[i]);
    for(int i=1; i<=m; i++) if(b[i]==b[i+m])
    {
        printf("the evil panda is lying again\n");
        return 0;
    }
    printf("panda is telling the truth...\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值