[POJ 3207]Ikki's Story IV - Panda's Trick(2-SAT入门题)

题目链接

http://poj.org/problem?id=3207

题目大意

思路

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

#define MAXE 251000
#define MAXV 251000

using namespace std;

int n,m;

struct edge
{
    int u,v,next;
}edges[MAXE];

int head[MAXV],nCount=0;

void AddEdge(int U,int V)
{
    edges[++nCount].u=U;
    edges[nCount].v=V;
    edges[nCount].next=head[U];
    head[U]=nCount;
}

void add(int u,int typeu,int v,int typev)
{
    AddEdge(((u<<1)+typeu)^1,(v<<1)+typev);
    AddEdge(((v<<1)+typev)^1,(u<<1)+typeu);
}

int dfn[MAXV],low[MAXV],dfs_time=0;
int belong[MAXV],scc_tot=0;
bool inStack[MAXV];
int stack[2*MAXV],top=0;

void TarjanSCC(int u)
{
    dfn[u]=low[u]=++dfs_time;
    stack[++top]=u;
    inStack[u]=true;
    for(int p=head[u];p!=-1;p=edges[p].next)
    {
        int v=edges[p].v;
        if(!dfn[v])
        {
            TarjanSCC(v);
            low[u]=min(low[u],low[v]);
        }
        else if(inStack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        scc_tot++;
        while(1)
        {
            int now=stack[top];
            inStack[now]=false;
            belong[now]=scc_tot;
            top--;
            if(now==u) break;
        }
    }
}

int st[MAXV],ed[MAXV]; //每条线段的起点与终点

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        nCount=dfs_time=scc_tot=top=0;
        memset(head,-1,sizeof(head));
        memset(belong,0,sizeof(belong));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(inStack,0,sizeof(inStack));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&st[i],&ed[i]);
            if(st[i]>ed[i]) swap(st[i],ed[i]);
        }
        for(int i=1;i<=m;i++)
            for(int j=i+1;j<=m;j++)
            {
                if((st[i]>=st[j]&&ed[i]>=ed[j]&&st[i]<=ed[j])||(st[i]<=st[j]&&ed[i]<=ed[j]&&st[j]<=ed[i]))
                {
                    add(i,0,j,0);
                    add(i,1,j,1);
                }
            }
        for(int i=2;i<=(m<<1)+1;i++)
            if(!dfn[i])
                TarjanSCC(i);
        bool flag=true;
        for(int i=2;i<=(m<<1)+1;i+=2)
            if(belong[i]==belong[i+1])
            {
                flag=false;
                break;
            }
        if(flag) puts("panda is telling the truth...");
        else puts("the evil panda is lying again");
    }
    return 0;
}

你可能感兴趣的:([POJ 3207]Ikki's Story IV - Panda's Trick(2-SAT入门题))