poj 3207 Ikki's Story IV - Panda's Trick(2-sat简单应用)

Ikki's Story IV - Panda's Trick
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 6038 Accepted: 2221

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

Source

题目:http://poj.org/problem?id=3207

题意:给你一个环,环上有n个点,然后给你m对点,要求你连接着m对点,使得连接的线没有交点,线可以在圆内或圆外

分析:连线只有两种状态,要么在圆内,要么在外面,只能选一个状态,如果两条连线可能出现相交,则必须保证一条在圆内,一条在外,到这里我们就可以发现这是个2-sat模型。。。。直接建图套模板= =

PS:判断两条连线是否会相交这题要注意下,我因为这个弄错导致wa了,下面的数据参考下

6 3
0 3
1 4
2 5


8 4
0 4
1 7
2 6
3 5

代码:

#include<cstdio>
#include<iostream>
using namespace std;
const int mm=555555;
const int mn=1111;
int ver[mm],next[mm];
int head[mn],dfn[mn],low[mn],q[mn],id[mn],s[mn],t[mn];
int i,j,k,n,m,edge,idx,top,cnt;
void add(int u,int v)
{
    ver[edge]=v,next[edge]=head[u],head[u]=edge++;
}
void dfs(int u)
{
    dfn[u]=low[u]=++idx;
    q[top++]=u;
    for(int i=head[u],v;i>=0;i=next[i])
        if(!dfn[v=ver[i]])
            dfs(v),low[u]=min(low[u],low[v]);
        else if(!id[v])low[u]=min(low[u],dfn[v]);
    if(dfn[u]==low[u])
    {
        id[u]=++cnt;
        while(q[--top]!=u)id[q[top]]=cnt;
    }
}
void Tarjan()
{
    for(cnt=idx=top=i=0;i<m+m;++i)dfn[i]=id[i]=0;
    for(i=0;i<m+m;++i)
        if(!dfn[i])dfs(i);
}
bool ok()
{
    for(int i=0;i<m+m;i+=2)
        if(id[i]==id[i^1])return 0;
    return 1;
}
bool Inter(int i,int j)
{
    if(i==j)return 0;
    if(s[i]>s[j]&&s[i]<t[j]&&t[i]>t[j])return 1;
    if(t[i]>s[j]&&t[i]<t[j]&&s[i]<s[j])return 1;
    return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(edge=i=0;i<m+m;++i)head[i]=-1;
        for(i=0;i<m;++i)
        {
            scanf("%d%d",&s[i],&t[i]);
            if(s[i]>t[i])swap(s[i],t[i]);
        }
        for(i=0;i<m;++i)
            for(j=0;j<m;++j)
                if(Inter(i,j))
                {
                    add(i<<1,j<<1|1);
                    add(i<<1|1,j<<1);
                }
        Tarjan();
        if(ok())puts("panda is telling the truth...");
        else puts("the evil panda is lying again");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值