POJ 3207 Ikki's Story IV,pojikki

來源:互聯網
上載者:User

POJ 3207 Ikki's Story IV,pojikki

題目地址:POJ 3207

找好矛盾關係,矛盾關係是(2,5)和(3,6)這兩個只能一個在外邊,一個在裡邊,利用這個矛盾關係來建圖。

可以用在外邊和裡邊來當1和0,最後判斷每對是否出現矛盾。

代碼如下:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const int INF=0x3f3f3f3f;int head[1100], cnt, top, ans, index;int dfn[1100], low[1100], instack[1100], stak[1100], belong[1100];struct node{    int u, v, next;}edge[1000000];struct N{    int l, r;}xian[10000];void add(int u, int v){    edge[cnt].v=v;    edge[cnt].next=head[u];    head[u]=cnt++;}int pan(N x, N y){    if((x.l>y.l&&x.l<y.r&&x.r>y.r)||(x.r>y.l&&x.r<y.r&&x.l<y.l))        return 1;    return 0;}void tarjan(int u){    dfn[u]=low[u]=++index;    instack[u]=1;    stak[++top]=u;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(!dfn[v])        {            tarjan(v);            low[u]=min(low[u],low[v]);        }        else if(instack[v])        {            low[u]=min(dfn[v],low[u]);        }    }    if(dfn[u]==low[u])    {        ans++;        while(1)        {            int v=stak[top--];            instack[v]=0;            belong[v]=ans;            if(u==v) break;        }    }}void init(){    memset(head,-1,sizeof(head));    cnt=top=ans=index=0;    memset(dfn,0,sizeof(dfn));    memset(instack,0,sizeof(instack));}int main(){    int n, m, i, j;    scanf("%d%d",&n,&m);    init();    for(i=0;i<m;i++)    {        scanf("%d%d",&xian[i].l,&xian[i].r);        if(xian[i].l>xian[i].r) swap(xian[i].l,xian[i].r);    }    for(i=0;i<m;i++)    {        for(j=0;j<i;j++)        {            if(pan(xian[i],xian[j]))            {                add(i<<1,j<<1|1);                add(j<<1,i<<1|1);                add(i<<1|1,j<<1);                add(j<<1|1,i<<1);                //printf("%d %d\n%d %d\n",i<<1,j<<1|1,j<<1,i<<1|1);            }        }    }    for(i=0;i<2*m;i++)    {        if(!dfn[i])            tarjan(i);    }    int flag=0;    for(i=0;i<m;i++)    {        if(belong[i<<1]==belong[i<<1|1])        {            flag=1;            //printf("%d\n",i);            break;        }    }    if(flag) puts("the evil panda is lying again");    else        puts("panda is telling the truth...");    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.