码迷,mamicode.com
首页 > 其他好文 > 详细

poj3207 Ikki's Story IV - Panda's Trick

时间:2014-12-15 23:15:31      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   ar   os   sp   for   on   div   

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...\n");
	else printf("the evil panda is lying again\n");
	return 0;
}

  

poj3207 Ikki's Story IV - Panda's Trick

标签:des   blog   io   ar   os   sp   for   on   div   

原文地址:http://www.cnblogs.com/zhber/p/4165948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!