zoukankan      html  css  js  c++  java
  • poj 3207 Ikki's Story IV

    Ikki's Story IV - Panda's Trick

     POJ - 3207 

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

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define maxn 1010
    using namespace std;
    int n,m,num,head[maxn],dfn[maxn],low[maxn],cnt,st[maxn],group,top,belong[maxn];
    bool in[maxn];
    int x[maxn],y[maxn];
    struct node{int to,pre;}e[maxn*maxn];
    void Insert(int from,int to){
        e[++num].to=to;
        e[num].pre=head[from];
        head[from]=num;
    }
    bool check(){
        for(int i=1;i<=m;i++)
            if(belong[i]==belong[i+m])return 0;
        return 1;
    }
    void Tarjan(int u){
        in[u]=1;st[++top]=u;
        dfn[u]=low[u]=++cnt;
        for(int i=head[u];i;i=e[i].pre){
            int v=e[i].to;
            if(!dfn[v]){
                Tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(in[v])
                low[u]=min(low[u],dfn[v]);
        }
        if(dfn[u]==low[u]){
            group++;
            while(st[top]!=u){
                in[st[top]]=0;
                belong[st[top]]=group;
                top--;
            }
            top--;
            in[u]=0;belong[u]=group;
        }
    }
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&x[i],&y[i]);
            x[i]++;y[i]++;
            if(x[i]>y[i])swap(x[i],y[i]);
        }
        for(int i=1;i<=m;i++)
            for(int j=i+1;j<=m;j++)
            if((x[i]<=x[j]&&y[i]>=x[j]&&y[i]<=y[j])||(x[i]>=x[j]&&x[i]<=y[j]&&y[i]>=y[j])){
                Insert(i,j+m);
                Insert(j,i+m);
                Insert(i+m,j);
                Insert(j+m,i);
            }
        n=2*m;
        for(int i=1;i<=n;i++)
            if(!dfn[i])Tarjan(i);
        if(check())puts("panda is telling the truth...");
        else puts("the evil panda is lying again");
        return 0;
    }
  • 相关阅读:
    929. 独特的电子邮件地址
    [工具.tcp]测试TCP通讯的网络延迟
    [技巧.Dotnet]轻松实现“强制.net程序以管理员身份运行”。
    [问题记录.VisualStudio]VS2013无法新增和打开项目
    [问题记录.VisualStudio]TFS项目映射问题解决
    [问题记录.dotnet]取网卡信息报错"找不到"-WMI
    模型驱动的数学原理
    剑指OFFER 旋转数组的最小数字
    剑指OFFER 用两个栈实现队列
    剑指OFFER 按之字形顺序打印二叉树
  • 原文地址:https://www.cnblogs.com/thmyl/p/8979277.html
Copyright © 2011-2022 走看看