HDU 3207 Ikki's Story IV-panda's trick (graph theory-2sat, graph theory-Tarjan)

Source: Internet
Author: User

Ikki's Story IV-panda's trick
Time limit:1000 ms   Memory limit:131072 K
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 areNPoints on its boundary in circular border: 0, 1, 2 ,...,N? 1. edevil panda claims that he is ing 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, cycle T 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 two integers:NAndM(N≤ 1,000,M≤ 500). The followingMLines each contain two integersAIAndBi, Which denote the endpoints ofITh 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

Question:

N points, M line segments, and line segments can be placed outside and inside the ring and asked if there is a solution that does not overlap.


Solution:

The 2sat method is used to determine whether a conflicting edge exists after Tarjan is finished based on the link edge of the conflicting link.


Solution code:

#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;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.