首页 > 代码库 > POJ3207-Ikki's Story IV - Panda's Trick(2-SAT)

POJ3207-Ikki's Story IV - Panda's Trick(2-SAT)

Ikki‘s Story IV - Panda‘s Trick
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 7841 Accepted: 2900

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...
题意:圆上有n个点,现在要连m条边,连边的方式有两种,一种是从圆外连,也可以从圆内连,然后边是不能相交的,问你是否有合法的连边方案。
思路:2-SAT,对于可能相交的两条边,分别连i+m->j,i->j+m,j->i+m,j+m->i即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 1000+10;
struct ee{
    int u,v;
}ee[maxn];

struct edge{
    int v,nxt;
}e[maxn*maxn];
int n,m;
int head[maxn];
int nume;
int lown[maxn],dfn[maxn];
int dfs_clk;
stack<int> S;
int scc_cnt;
int sccno[maxn];

void init(){
    memset(head,0,sizeof head);
    memset(dfn,0,sizeof dfn);
    memset(sccno,0,sizeof sccno);
    nume = 1;
    dfs_clk = scc_cnt = 0;
    while(!S.empty()) S.pop();
}

void addedge(int u,int v){
    e[++nume].nxt = head[u];
    e[nume].v = v;
    head[u] = nume;
}

void Tarjan(int u){
    lown[u] = dfn[u] = ++dfs_clk;
    S.push(u);
    for(int i = head[u]; i ; i = e[i].nxt){
        int v = e[i].v;
        if(!dfn[v]){
            Tarjan(v);
            lown[u] = min(lown[u],lown[v]);
        }else if (!sccno[v]){
            lown[u] = min(lown[u],dfn[v]);
        }
    }
    if(lown[u] == dfn[u]){
        scc_cnt++;
        while(true){
            int x = S.top(); S.pop();
            sccno[x] = scc_cnt;
            if(x==u) break;
        }
    }
}
inline bool touch(int i,int j){
    return  (ee[i].u < ee[j].u && ee[i].v > ee[j].u && ee[j].v > ee[i].v) || (ee[j].u < ee[i].u && ee[j].v > ee[i].u && ee[i].v > ee[j].v);
}
int main(){

    while(~scanf("%d%d",&n,&m)){
        init();
        int a,b;
        for(int i = 1; i <= m; i++){
            scanf("%d%d",&ee[i].u,&ee[i].v);
            if(ee[i].u > ee[i].v) swap(ee[i].u,ee[i].v);
        }
        for(int i = 1; i <= m; i++){
            for(int j = i+1; j <= m; j++){
                if(touch(i,j)){
                    addedge(i,j+m);
                    addedge(j+m,i);
                    addedge(i+m,j);
                    addedge(j,i+m);
                }
            }
        }
        for(int i = 1; i <= 2*m; i++){
            if(dfn[i]==0)
                Tarjan(i);
        }
        bool flag = true;
        for(int i = 1; i <= m; i++){
            if(sccno[i] == sccno[i+m]){
                flag = false;
                break;
            }
        }
        if(flag){
            printf("panda is telling the truth...\n");
        }else{
            printf("the evil panda is lying again\n");
        }
    }
    return 0;
}