POJ 3207 2-SAT

Ikki's Story IV - Panda's Trick
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 8982   Accepted: 3303

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



雖然kb大神(orz)說這是一道2-sat的水題

但是我覺得這道題如果能理解了對2-sat算法有很好的理解


他的意思是。線是不能相交的。只能從圓的內部走或者從圓的外部走。給你N條線段。問你這些線段可否按照前面的規則畫出來

假設一個圓上有4個點  1 3 連在一起 2 4 連在一起  那麼1 3 和2 4 不能同側 

於是就應該判斷一下 如果這兩條線不能在圓的一邊 那麼就應該把它們分開來 (代碼中的那一長串的IF判斷)


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <cstdlib>  
#include <algorithm>  
using namespace std;  
  
  
  
//HDU3207  
//******************************************   
//2-SAT 強連通縮點   
const int MAXN = 2010;   
const int MAXM = 4000010;   
struct Edge   
{   
    int to,next;   
}edge[MAXM];   
int head[MAXN],tot;   
void init()   
{   
    tot = 0;   
    memset(head,-1,sizeof(head));   
}   
void addedge(int u,int v)   
{   
    edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++;   
}   
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong數組的值1~scc   
int Index,top;   
int scc;   
bool Instack[MAXN];   
int num[MAXN];   
void Tarjan(int u)   
{   
    int v;   
    Low[u] = DFN[u] = ++Index;   
    Stack[top++] = u;   
    Instack[u] = true;   
    for(int i = head[u];i != -1;i = edge[i].next)   
    {   
        v = edge[i].to;   
        if( !DFN[v] )   
        {   
            Tarjan(v);   
            if(Low[u] > Low[v])Low[u] = Low[v];   
        }   
        else if(Instack[v] && Low[u] > DFN[v])   
            Low[u] = DFN[v];   
    }   
    if(Low[u] == DFN[u])   
    {   
        scc++;   
        do   
        {   
            v = Stack[--top];   
            Instack[v] = false;   
            Belong[v] = scc;   
            num[scc]++;   
        }   
        while(v != u);   
    }   
}   
  
bool solvable(int n)//n是總個數,需要選擇一半   
{   
    memset(DFN,0,sizeof(DFN));   
    memset(Instack,false,sizeof(Instack));   
    memset(num,0,sizeof(num));   
    Index = scc = top = 0;   
    for(int i = 0;i < n;i++)   
        if(!DFN[i])   
            Tarjan(i);   
    //for(int i = 0;i < n;i += 2) printf("belong[%d] = %d\n belong[%d] = %d\n",i,Belong[i],i^1,Belong[i^1]);  
    for(int i = 0;i < n;i += 2)   
    {   
        if(Belong[i] == Belong[i^1])   
        return false;   
    }   
    return true;   
}   
//*************************************************   
   
//拓撲排序求任意一組解部分   
/*  
queue<int>q1,q2;   
vector<vector<int> > dag;//縮點後的逆向DAG圖   
char color[MAXN];//染色,爲'R'是選擇的   
int indeg[MAXN];//入度   
int cf[MAXN];   
void solve(int n)   
{   
    dag.assign(scc+1,vector<int>());   
    memset(indeg,0,sizeof(indeg));   
    memset(color,0,sizeof(color));   
    for(int u = 0;u < n;u++)   
    for(int i = head[u];i != -1;i = edge[i].next)   
    {   
        int v = edge[i].to;   
        if(Belong[u] != Belong[v])   
        {   
            dag[Belong[v]].push_back(Belong[u]);   
            indeg[Belong[u]]++;   
        }   
    }   
    for(int i = 0;i < n;i += 2)   
    {   
        cf[Belong[i]] = Belong[i^1];   
        cf[Belong[i^1]] = Belong[i];   
    }   
    while(!q1.empty())q1.pop();   
    while(!q2.empty())q2.pop();   
    for(int i = 1;i <= scc;i++)   
        if(indeg[i] == 0)   
    q1.push(i);   
    while(!q1.empty())   
    {   
        int u = q1.front();   
        q1.pop();   
        if(color[u] == 0)   
        {   
            color[u] = 'R';   
            color[cf[u]] = 'B';   
        }   
        int sz = dag[u].size();   
        for(int i = 0;i < sz;i++)   
        {   
            indeg[dag[u][i]]--;  
            if(indeg[dag[u][i]] == 0)   
            q1.push(dag[u][i]);   
        }   
    }   
}   
 */  
  
int main()   
{   
    int n,m;   
    int a1,a2,c1,c2;
	int x[1111],y[1111];  
  	scanf("%d%d",&n,&m);
  	init();
  	for(int i=0;i<m;i++){
		scanf("%d%d",&x[i],&y[i]);
		if(x[i]>y[i]){
			int tem=x[i];
			x[i]=y[i];
			y[i]=tem;
		}
	}
	for(int i=0;i<m;i++){
		for(int j=i+1;j<m;j++){
			if((x[i]<=x[j]&&y[i]<=y[j]&&y[i]>=x[j])||(x[i]>=x[j]&&y[i]>=y[j]&&y[j]>=x[i])){  //判斷他是否能在一邊完成 
				addedge(i*2,j*2+1);
				addedge(j*2,i*2+1);
				addedge(i*2+1,j*2);
				addedge(j*2+1,j*2);
			}
		}
	}
	
   	if(solvable(m*2)){  
		printf("panda is telling the truth...\n");  
  	}  
   	else printf("the evil panda is lying again\n");  
    return 0;   
}   


發佈了80 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章