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

//

【解题思路】

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;
}
//添加边
{
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]))
{
}
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");
}
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：poj 3207 Ikki's Story 2-SAT 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)