poj_3207_Ikki's Story IV - Panda's Trick(2-sat+Kosaraju)

本文介绍了一种使用2-SAT算法解决逻辑问题的方法,并通过一个具体例子详细展示了如何构建图模型、进行深度优先搜索以及判断解决方案是否存在。文章还提供了完整的C++代码实现。
Ikki's Story IV - Panda's Trick
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 9278 Accepted: 3416

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


第一次做2-sat的题,
通过 大神博客学习了一下。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 1010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

bool visit[maxn];

vector<vector<int > >adj;//原图
vector<vector<int > >radj;//逆图

int id[maxn],order[maxn];//强连通分量,访问顺序

int n,cnt;

struct Node
{
    int l,r;
}node[maxn];

void dfs(int u)
{
    visit[u]=true;
    int i,len=adj[u].size();
    for(i=0;i<len;i++)
    {
        if(!visit[adj[u][i]])
        dfs(adj[u][i]);
    }
    order[cnt++]=u;
}

void rdfs(int u)
{
    visit[u]=true;
    int i,len=radj[u].size();
    id[u]=cnt;
    for(i=0;i<len;i++)
    {
        if(!visit[radj[u][i]])
        rdfs(radj[u][i]);
    }
}

void korasaju()
{
    int i;
    mem(visit,false);

    for(cnt=0,i=0;i<2*n;i++)
        if(!visit[i])
            dfs(i);

    mem(id,0);
    mem(visit,false);

    for(cnt=0,i=2*n-1;i>=0;i--)
    {
        if(!visit[order[i]])
        {
             cnt++;
             rdfs(order[i]);
        }
    }
}

int solve()
{
    int i;
    for(i=0;i<n;i++)
    {
        if(id[2*i]==id[2*i+1])
        return 0;
    }
    return 1;
}
int main()
{
    int i,j,N,x,y;
    while(~scanf("%d%d",&N,&n))
    {
        for(i=0;i<n;i++)
        {
            Si(x),Si(y);
            if(x>y)swap(x,y);
            node[i].l=x;
            node[i].r=y;
        }
        adj.assign(2*n,vector<int>());//void assign(size_type n,const T& x = T());
        radj.assign(2*n,vector<int>()); //赋n个值为x的元素到vector容器中,这个
                                        //容器会清除掉vector容器中以前的内容

        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if((node[i].l<node[j].l&&node[i].r>node[j].l&&node[i].r<node[j].r)||
                    (node[j].l<node[i].l&&node[j].r>node[i].l&&node[j].r<node[i].r))
                {
                    adj[2*i].push_back(2*j+1);
                    adj[2*j+1].push_back(2*i);
                    adj[2*j].push_back(2*i+1);
                    adj[2*i+1].push_back(2*j);
                    radj[2*i].push_back(2*j+1);
                    radj[2*j+1].push_back(2*i);
                    radj[2*j].push_back(2*i+1);
                    radj[2*i+1].push_back(2*j);
                }
            }
        }
        korasaju();
        if(solve())Ps("panda is telling the truth...");
        else Ps("the evil panda is lying again");

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值