poj 3207 Ikki's Story IV

来源:互联网 发布:世界银行网站数据库 编辑:程序博客网 时间:2024/05/07 22:25

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 20 13 2

Sample Output

panda is telling the truth...

Source

POJ Monthly--2007.03.04, Ikki


题意:平面上,一个圆,圆的边上按顺时针放着n个点。现在要连m条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接。给你的信息中,每个点最多只会连接的一条边。问能不能连接这m条边,使这些边都不相交。

思路:

转化成标准的2-sta问题:
1:每个边看成2个点:分别表示在内部连接和在外部连接,只能选择一个。计作点i和点i'
2:如果两条边i和j必须一个画在内部,一个画在外部(一个简单判断就可以)
那么连边:
i->j’, 表示i画内部的话,j只能画外部,即j’

判断图的强连通;


代码:

#include<cstdio>#include<cstring>#include<algorithm>#include<stack>using namespace std;stack<int>s;const int N=505;const int M=1000005;struct node{    int a,b;}str[N];struct node1{    int now,next;}nd[M];int head[2*N],dfs[2*N],ins[2*N],belong[2*N],low[2*N];int tot,tol,ans;bool cmp(node x,node y){    return x.a<y.a;}void add(int x,int y){    nd[tot].now=y;    nd[tot].next=head[x];    head[x]=tot++;    nd[tot].now=x;    nd[tot].next=head[y];    head[y]=tot++;}void tarjan(int u){    ins[u]=1;    dfs[u]=low[u]=tol++;    s.push(u);    for(int i=head[u];i!=-1;i=nd[i].next)    {        int v=nd[i].now;        if(dfs[v]==-1)        {            tarjan(v);            low[u]=min(low[u],low[v]);        }        else if(ins[v])            low[u]=min(low[u],dfs[v]);    }    if(dfs[u]==low[u])    {        while(1)        {            int q=s.top();            s.pop();            ins[q]=1;            belong[q]=ans;            if(q==u) break;        }        ans++;    }}bool ok(int n){    memset(dfs,-1,sizeof(dfs));    memset(ins,0,sizeof(ins));    tol=0;ans=0;    for(int i=0;i<2*n;i++)        if(dfs[i]==-1)            tarjan(i);    for(int i=0;i<n;i++)        if(belong[i]==belong[i+n])            return false;    return true;}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        tot=0;        memset(head,-1,sizeof(head));        for(int i=0;i<m;i++)        {            int a,b;            scanf("%d%d",&a,&b);            if(a<b)            {                str[i].a=a;str[i].b=b;            }            else            {                str[i].a=b;str[i].b=a;            }        }        sort(str,str+m,cmp);  //为了后面容易判断预处理        for(int i=0;i<m-1;i++)            for(int j=i+1;j<m;j++)            {                if(str[i].b>str[j].a&&str[i].b<str[j].b)                {                    add(i,j+m);                    add(j,i+m);                }            }        if(ok(m)) printf("panda is telling the truth...\n");        else printf("the evil panda is lying again\n");    }}


0 0
原创粉丝点击