poj 3207 Ikki's Story 2-SAT

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条边,比如a,b,那么a到b可以从平面正面连接,也可以从平面另一面连接。给你的信息中,每个点最多只会连接的一条边。问能不能连接这m条边,使这些边都不相交。

【解题思路】

每个边看成2个点:分别表示在正面连接和在反面连接,只能选择一个。表示为点i和点i'

如果两条边i和j必须一个画在正面,一个画在反面,有矛盾,那么连边:

i->j’, 表示i画正面的话,j只能画反面,即j’

j->i’,同理

i’->j,同理

j’->i,同理



#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=100000;
int V,E;//点数(1) 边数
struct edge//邻接表
{
    int t,w;//u->t=w;
    int next;
};
int p[maxn];//表头节点
edge G[maxn];
int l;
void init()
{
    memset(p,-1,sizeof(p));
    l=0;
}
//添加边
void addedge(int u,int t,int w)//u->t=w;
{
    G[l].w=w;
    G[l].t=t;
    G[l].next=p[u];
    p[u]=l++;
}
//tarjan算法 求有向图强联通分量
int dfn[maxn],lowc[maxn];
//dfn[u]节点u搜索的次序编号,lowc[u]u或者u的子树能够追溯到的栈中的最早的节点
int belg[maxn];//第i个节点属于belg[i]个强连通分量
int stck[maxn],stop;//stck栈
int instck[maxn];//第i个节点是否在栈中
int scnt;//强联通分量
int index;
void dfs(int i)
{
    dfn[i]=lowc[i]=++index;
    instck[i]=1;//节点i入栈
    stck[++stop]=i;
    for(int j=p[i];j!=-1;j=G[j].next)
    {
        int t=G[j].t;
        //更新lowc数组
        if(!dfn[t])//t没有遍历过
        {
        dfs(t);
        if(lowc[i]>lowc[t]) lowc[i]=lowc[t];
        }//t是i的祖先节点
        else if(instck[t]&&lowc[i]>dfn[t]) lowc[i]=dfn[t];
    }
    //是强连通分量的根节点
    if(dfn[i]==lowc[i])
    {
    scnt++;
    int t;
    do
    {
        t=stck[stop--];
        instck[t]=0;
        belg[t]=scnt;
        }while(t!=i);
    }
}
int tarjan()
{
    stop=scnt=index=0;
    memset(dfn,0,sizeof(dfn));
    memset(instck,0,sizeof(instck));
    for(int i=1;i<=V;i++)
    {
        if(!dfn[i]) dfs(i);
    }
    return scnt;
}
int e[maxn][2];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        init();V=2*m;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&e[i][0],&e[i][1]);
            if(e[i][0]>e[i][1])
            {
                e[i][0]=e[i][0]^e[i][1];
                e[i][1]=e[i][0]^e[i][1];
                e[i][0]=e[i][0]^e[i][1];
            }
        }
        for(int i=1;i<=m;i++)
            for(int j=1;j<=m;j++)
                if((e[i][0]<e[j][0]&&e[i][1]<e[j][1]&&e[i][1]>e[j][0])||(e[i][0]>e[j][0]&&e[i][1]>e[j][1]&&e[i][1]<e[j][0]))
                {
                    addedge(i,j+m,1);
                    addedge(j,i+m,1);
                    addedge(i+m,j,1);
                    addedge(j+m,i,1);
                }
        tarjan();
        bool flag=false;
        for(int i=1;i<=m;i++)
        {
            if(belg[i]==belg[i+m])
            {
                printf("the evil panda is lying again\n");
                flag=true;
                break;
            }
        }
        if(!flag) printf("panda is telling the truth...\n");
    }
}


发布了743 篇原创文章 · 获赞 5 · 访问量 69万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章