POJ 3207 2-sat

来源:互联网 发布:mac上用的无线鼠标 编辑:程序博客网 时间:2024/04/27 22:38
Ikki's Story IV - Panda's Trick
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 3680 Accepted: 1330

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 andm (n ≤ 1,000, m ≤ 500). The following m lines each contain two integersai and bi, which denote the endpoints of theith 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
以连接的线为点,然后点为边,建图
因为一个点只能连一次,所以这个点要么从内部连接,要么从外部连接,没有第三种可能,所以每一个点可以简单的想象成两个点
p和p'这两个点只能有一个点出现在图中,所以这个题便转化为了那篇名为《利用对称性解决2-sat》的论文中的那个和平委员会的题目
利用强连通分量(本人用tarjan实现)就可以解决了
#include<stdio.h>#include<algorithm>#include<stack>#include<vector>using namespace std;struct line{int s;int t;};vector<int>map[1005];int belong[1005],dfn[1005],low[1005];bool instack[1005],used[1005];stack<int>s;int index,cnt,n,m;line l[505];void init(){int i;for(i=0;i<1005;i++)map[i].clear();memset(dfn,-1,sizeof(dfn));memset(used,0,sizeof(used));memset(low,0,sizeof(low));memset(belong,0,sizeof(belong));memset(instack,0,sizeof(instack));index=0;cnt=0;}int min(int a,int b){if(a>b)return b;elsereturn a;}void tarjan(int u){int i,v;index++;dfn[u]=index;low[u]=index;instack[u]=true;s.push(u);used[u]=true;for(i=0;i<map[u].size();i++){v=map[u][i];if(!used[v]){tarjan(v);low[u]=min(low[u],low[v]);}else if(instack[v]){low[u]=min(low[u],dfn[v]);}}if(dfn[u]==low[u]){cnt++;do{v=s.top();s.pop();belong[v]=cnt;instack[v]=false;}while(u!=v);}}int main(){int i,j;init();scanf("%d%d",&n,&m);for(i=1;i<=m;i++){scanf("%d%d",&l[i].s,&l[i].t);if(l[i].s>l[i].t)swap(l[i].s,l[i].t);}for(i=1;i<=m;i++)for(j=1;j<=m;j++)if((l[i].t<l[j].t&&l[i].s<l[j].s&&l[i].t>l[j].s)||(l[i].t>l[j].t&&l[i].s>l[j].s&&l[i].s<l[j].t)){map[i].push_back(j+m);map[i+m].push_back(j);map[j].push_back(i+m);map[j+m].push_back(i);}for(i=1;i<=2*m;i++){if(dfn[i]==-1)tarjan(i);}for(i=1;i<=m;i++){if(belong[i]==belong[i+m]){printf("the evil panda is lying again/n");return 0;}}printf("panda is telling the truth.../n");return 0;}