• poj3207 Ikki's Story IV


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

    2-sat的第一题……主要是写一写有感觉
    #include<cstdio>
    #include<iostream>
    using namespace std;
    #define LL long long
    #define N 101000
    #define M 510000
    inline LL read()
    {
        LL x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    struct edge{int to,next;}e[2*M];
    int n,m,cnt,cnt3,tt;
    int head[N];
    int a[N],b[N],inset[N];
    int dfn[N],low[N],belong[N];
    int top,zhan[N];
    inline void ins(int u,int v)
    {
    	e[++cnt].to=v;
    	e[cnt].next=head[u];
    	head[u]=cnt;
    }
    inline void insert(int u,int v)
    {
    	ins(u,v);
    	ins(v,u);
    }
    inline void dfs(int x)
    {
    	zhan[++top]=x;inset[x]=1;
    	dfn[x]=low[x]=++tt;
    	for (int i=head[x];i;i=e[i].next)
    	{
    		if (!dfn[e[i].to])
    		{
    			dfs(e[i].to);
    			low[x]=min(low[x],low[e[i].to]);
    		}else if (inset[e[i].to])
    		{
    			low[x]=min(low[x],dfn[e[i].to]);
    		}
    	}
    	if (dfn[x]==low[x])
    	{
    		cnt3++;
    		int p=-1;
    		while (p!=x)
    		{
    			p=zhan[top--];
    			belong[p]=cnt3;
    			inset[p]=0;
    		}
    	}
    }
    inline void tarjan()
    {
    	for (int i=1;i<=2*m;i++)if(!dfn[i])dfs(i);
    }
    inline bool jud()
    {
    	for(int i=1;i<=m;i++)if (belong[2*i]==belong[2*i-1])return 0;
    	return 1;
    }
    inline void buildmap()
    {
    	for(int i=1;i<=m;i++)
    		for(int j=i+1;j<=m;j++)
    			if((a[i]<a[j]&&a[j]<b[i]&&b[j]>b[i])||(a[j]<a[i]&&a[i]<b[j]&&b[i]>b[j]))
    			{
    				insert(2*i-1,2*j);
    				insert(2*j-1,2*i);
    			}
    }
    int main()
    {
    	n=read();m=read();
    	for (int i=1;i<=m;i++)
    	{
    		a[i]=read();
    		b[i]=read();
    		if(a[i]>b[i])swap(a[i],b[i]);
    	}
    	buildmap();
    	tarjan();
    	if (jud())printf("panda is telling the truth...
    ");
    	else printf("the evil panda is lying again
    ");
    	return 0;
    }
    

      

    ——by zhber,转载请注明来源
  • 相关阅读:
    sqlite3 多线程和锁 ,优化插入速度及性能优化
    Android Touch事件传递机制通俗讲解
    26. Remove Duplicates from Sorted Array----Array----Easy----20160927
    88. Merge Sorted Array----Array----Easy----20160926
    @121. Best Time to Buy and Sell Stock----Array----Easy---20160926
    88. Merge Sorted Array----Array----Easy----20160925
    189. Rotate Array----Array----Easy----20160925
    169. Majority Element----Array----Easy----20160925
    219. Contains Duplicate II 超时
    c++Map容器 详解
  • 原文地址:https://www.cnblogs.com/zhber/p/4165948.html
Copyright © 2020-2023  润新知