poj3207 Ikki's Story IV - Panda's Trick

Ikki's Story IV - Panda's Trick
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 10448   Accepted: 3829

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模板题,每条边要么在圆里面,要么在圆外面,而且不能选重叠的,我们只需要拆点连边然后tarjan看同一集合的两个点是不是在同一个强连通分量里就好了.
如果对2-SAT问题不是很了解,推荐看一篇博文:传送门
 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>

using namespace std;

const int maxn = 1000010;
 
int n,m,a[maxn],b[maxn],pre[maxn],scc[maxn],top,low[maxn],dfs_clock,head[maxn],nextt[maxn],to[maxn],tot = 1;

void add(int x,int y)
{
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;
}

stack <int> s;

void tarjan(int u)
{
    low[u] = pre[u] = ++dfs_clock;
    s.push(u);
    for (int i = head[u];i;i = nextt[i])
    {
        int v = to[i];
        if (!pre[v])
        {
            tarjan(v);
            low[u] = min(low[u],low[v]);
        }
        else
        if (!scc[v])
        low[u] = min(low[u],pre[v]);
    }
    if (low[u] == pre[u])
    {
        top++;
        while (1)
        {
            int t = s.top();
            s.pop();
            scc[t] = top;
            if (t == u)
            break;
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    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 ] && b[ i ] > a[ j ] && b[ i ] < b[ j ] || b[ i ] > b[ j ] && a[ i ] > a[ j ] && a[ i ] < b[ j ])
    {
        add(i * 2,j * 2 + 1);
        add(j * 2 + 1,i * 2);
        add(i * 2 + 1,j * 2);
        add(j * 2,i * 2 + 1);
    }
    for (int i = 2; i <= m * 2 + 1; i++)
    if (!pre[i])
    tarjan(i);
    for (int i = 2; i <= m * 2; i+=2)
    if (scc[i] == scc[i + 1])
    {
        printf( "the evil panda is lying again" ); 
        return 0;
    }
    printf("panda is telling the truth..." );

    return 0;
}

 

posted @ 2017-09-15 15:45  zbtrs  阅读(263)  评论(0编辑  收藏  举报