首页 > 代码库 > HDU 3207 Ikki's Story IV - Panda's Trick(图论-2SAT,图论-tarjan)

HDU 3207 Ikki's Story IV - Panda's Trick(图论-2SAT,图论-tarjan)

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

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

Source

POJ Monthly--2007.03.04, Ikki

题目大意:

n个点,m条线段,线段可以放在环的外面和里面,问是否找到不相交的方案。


解题思路:

用2SAT的方法,根据矛盾关系连边,最后tarjan完后判断是否有矛盾边存在。


解题代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int maxm=510000;
const int maxn=1100;

struct edge{
    int u,v,next;
    edge(int u0=0,int v0=0){
        u=u0;v=v0;
    }
}e[maxm];

int n,m,head[maxn],color[maxn],dfn[maxn],low[maxn],cnt,nc,index;
bool mark[maxn];
vector < pair<int,int> > v;
vector <int> vec;

void addedge(int u,int v){
    e[cnt]=edge(u,v);e[cnt].next=head[u];head[u]=cnt++;
}

void input(){
    vec.clear();
    cnt=nc=index=0;
    for(int i=0;i<=2*m;i++){
        color[i]=head[i]=-1;
        mark[i]=false;
        dfn[i]=0;
    }
    v.clear();
    v.resize(m);
    for(int i=0;i<m;i++){
        scanf("%d%d",&v[i].first,&v[i].second);
        if(v[i].first>v[i].second) swap(v[i].first,v[i].second);
    }
}

void tarjan(int s){
    dfn[s]=low[s]=++index;
    mark[s]=true;
    vec.push_back(s);
    for(int i=head[s];i!=-1;i=e[i].next){
        int d=e[i].v;
        if(!dfn[d]){
            tarjan(d);
            low[s]=min(low[d],low[s]);
        }else if(mark[d]){
            low[s]=min(low[s],dfn[d]);
        }
    }
    if(dfn[s]==low[s]){
        nc++;
        int d;
        do{
            d=vec.back();
            vec.pop_back();
            color[d]=nc;
            mark[d]=false;
        }while(d!=s);
    }
}

void solve(){
    for(int i=0;i<m;i++){
        for(int j=i+1;j<m;j++){
            if( v[i].first>=v[j].second || v[j].first>=v[i].second ) continue;
            if( ( v[i].first<=v[j].first && v[j].second<=v[i].second ) || ( v[j].first<=v[i].first && v[i].second<=v[j].second ) ) continue;
            addedge(i,j+m);
            addedge(i+m,j);
            addedge(j,i+m);
            addedge(j+m,i);
        }
    }
    for(int i=0;i<2*m;i++){
        if(!dfn[i]) tarjan(i);
    }
    for(int i=0;i<m;i++){
        if(color[i]==color[i+m]){
            printf("the evil panda is lying again\n");
            return;
        }
    }
    printf("panda is telling the truth...\n");
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        input();
        solve();
    }
    return 0;
}