码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3207 Ikki's Story IV - Panda's Trick 2-SAT

时间:2017-04-17 19:59:51      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:twosat   思路   nod   and   cstring   queue   san   gic   memory   

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

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

题目链接:http://poj.org/problem?id=3207

题意:给你一个圆,n个点,让你连接两个点,需要使得所有边没有相交;

   你可以再圆内连接或者圆外连接,即如果两直线相交,不可以同时在圆内或者圆外;

思路:2-SAT模板题,orz大佬,搞了一波板子;

   将边抽象成一个点,两边如果相交,不可以同时取1,或者取0;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e6+2,M=1e7+10,inf=1e9+10;
const ll INF=1e18+10,mod=1e9+7;

///   数组大小
const int MAXNODE = 100005 * 2; //两倍

struct TwoSat {
    int n;
    vector<int> g[MAXNODE];
    int pre[MAXNODE], dfn[MAXNODE], dfs_clock, sccn, sccno[MAXNODE];
    stack<int> S;
    int mark[MAXNODE];

    void init(int tot) {
        n = tot * 2;
        for (int i = 0; i < n; i+= 2) {
            g[i].clear();
            g[i^1].clear();
        }
    }

    void add_Edge(int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u);
    }

    void dfs_scc(int u) {
        pre[u] = dfn[u] = ++dfs_clock;
        S.push(u);
        for (int i = 0; i < g[u].size(); i++) {
            int v = g[u][i];
            if (!pre[v]) {
                dfs_scc(v);
                dfn[u] = min(dfn[u], dfn[v]);
            } else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
        }
        if (pre[u] == dfn[u]) {
            sccn++;
            while (1) {
                int x = S.top(); S.pop();
                sccno[x] = sccn;
                if (x == u) break;
            }
        }
    }

    void find_scc() {
        dfs_clock = sccn = 0;
        memset(sccno, 0, sizeof(sccno));
        memset(pre, 0, sizeof(pre));
        for (int i = 0; i < n; i++)
            if (!pre[i]) dfs_scc(i);
    }

    bool solve() {
        find_scc();
        for (int i = 0; i < n; i += 2) {
            if (sccno[i] == sccno[i + 1]) return false;
            mark[i / 2] = (sccno[i] > sccno[i + 1]);
        }
        return true;
    }
} gao;
int a[N],b[N];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        gao.init(n);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a[i],&b[i]);
            if(a[i]>b[i])swap(a[i],b[i]);
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=i+1;j<=m;j++)
            {
                if((a[i]<a[j]&&a[j]<b[i]&&b[j]>b[i])||(a[j]<a[i]&&a[i]<b[j]&&b[i]>b[j]))
                {
                    gao.add_Edge(2*i,2*j+1);
                    gao.add_Edge(2*i+1,2*j);
                }
            }
        }
        if(gao.solve())printf("panda is telling the truth...\n");
        else printf("the evil panda is lying again\n");
    }
    return 0;
}

 

poj 3207 Ikki's Story IV - Panda's Trick 2-SAT

标签:twosat   思路   nod   and   cstring   queue   san   gic   memory   

原文地址:http://www.cnblogs.com/jhz033/p/6724400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!