Ikki's Story IV - Panda's Trick

Ikki's Story IV - Panda's Trick
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 7821 Accepted: 2892
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

POJ Monthly--2007.03.04, Ikki


题意:就是有n个点组成m条线,是否有不相交的方案。

刚开始解这道题的时候,没有思路,查找各种资料,发现这是典型的2-SAT问题,推荐一篇论文《由对称性解2-SAT问题》。
由于一个点只能对应一个连线,所以每一个点只有两个选择,要么在圆内,要么在圆外。有的点却是互斥的,它们的连线不可能同时在圆内或者同时在圆外,因为这两条连线是不能相交的。将每条Link i看做一个点,如果Link在圆内,则选择i,如果在圆外,则选择i'。对于两条线(i,j),如果i,j不能同时在圆内,那么它们两个也不能同时在圆外。i,j不能同时在圆内,则有边(i,j'),(j,i'),(i',j),(j',i)。(2-SAT的构图方式决定)当图建完后,先求原图的强连通分量,并缩点,(将(i,i')称为一组),判断是否存在(i,i')属于同一组,若存在,则不可能,若不存在,则可能。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int V=1005;
int g[V][V];
int stk[V];
int num[V];
int id[V];
int low[V],pre[V];
int cnt,scnt,stop;
int m;

void addedge(int a,int b)
{
    if(g[a][b])
        g[a][b]=0;
    else
        g[a][b]=1;
}

void dfs(int v)
{
    int t,minc=low[v]=pre[v]=cnt++;
    stk[stop++]=v;
    for(int i=0;i<=2*m;i++)
    if(g[v][i])
    {
        if(pre[i]==-1)
            dfs(i);
        if(low[i]<minc)
            minc=low[i];


    }
    if(minc<low[v])
    {
        low[v]=minc;
        return ;
    }

    do{
        id[t=stk[--stop]]=scnt;
        low[t]=2*m+1;
    }while(t!=v);
    ++scnt;
}


void tarjan()
{
    stop=cnt=scnt=0;
    for(int i=0;i<=2*m;i++)
    if(pre[i]==-1)
        dfs(i);
}

int main()
{
    int n;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(g,0,sizeof(g));
        memset(pre,-1,sizeof(num));
        memset(low,-1,sizeof(low));
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(a>b)
                swap(a,b);
            num[a]=i+1;
            num[b]=i+1;
            for(int j=a+1;j<b;j++)
             if(num[j])
             {
                 addedge(2*i+1,2*j);
                 addedge(2*i+2,2*j-1);
             }
        }
        tarjan();
        bool  ans=true;
        for(int i=0;i<m;i++)
            if(id[2*i]==id[2*i+1])
            ans=false;
        if(ans)
            printf("panda is telling the true...\n");
        else
            printf("the evil panda is lying again\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值