POJ3207Ikki's Story IV

来源:互联网 发布:新闻的数据库设计 编辑:程序博客网 时间:2024/05/17 21:47

一叶落寞,万物失色。


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 andm (n ≤ 1,000,m ≤ 500). The following m lines each contain two integersai andbi, which denote the endpoints of theith 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 20 13 2

Sample Output

panda is telling the truth...

题意:

一个圆上顺时针放着n个点,现在要连m条边,每条边可以从圆的内部连也可以从圆的外部连。

保证每个点最多连1条边,问是否能使所有的边都不相交。

思路:

典型的2-sat问题。

建图:对于一条边i,在圆内记为i,在圆外记为i'

设边i连接点A,B,边j连接点C,D。i与j在圆内是否相交就是线段AB与线段CD是否相交,用坐标判断一下。

可以证明,如果i与j在圆内不能共存,则在圆外也一定不能共存,即:

i在圆内,则j一定在圆外,建边i->j'

i在圆外,则j一定在圆内,建边i'->j

j在圆内,则i一定在圆外,建边j->i'

j在圆外,则i一定在圆内,建边j'->i

然后用2-sat判断是否可行。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#define MAXN 1005#define MAXM 2000005#define INF 1000000000using namespace std;int n, m, e, head[MAXN];int dfn[MAXN], low[MAXN], index, instack[MAXN], scc;int top, st[MAXN], fa[MAXN];int x[MAXN], y[MAXN];struct Edge{    int v, next;}edge[MAXM];void init(){    e = index = scc = top = 0;    memset(dfn, 0, sizeof(dfn));    memset(instack, 0, sizeof(instack));    memset(head, -1, sizeof(head));}void insert(int x, int y){    edge[e].v = y;    edge[e].next = head[x];    head[x] = e++;}void tarjan(int u){    int v;    instack[u] = 1;    st[++top] = u;    dfn[u] = low[u] = ++index;    for(int i = head[u]; i != -1; i = edge[i].next)    {        v = edge[i].v;        if(!dfn[v])        {            tarjan(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++;        do        {            v = st[top--];            instack[v] = 0;            fa[v] = scc;        }while(v != u);    }}void build(){    for(int i = 1; i <= m; i++)    {        scanf("%d%d", &x[i], &y[i]);        x[i]++; y[i]++;        if(x[i] > y[i]) swap(x[i], y[i]);    }    for(int i = 1; i <= m; i++)        for(int j = i + 1; j <= m; j++)            if((x[i] <= x[j] && y[i] >= x[j] && y[i] <= y[j]) || (x[i] >= x[j] && x[i] <= y[j] && y[i] >= y[j]))            {                insert(i, j + m);                insert(j, i + m);                insert(i + m, j);                insert(j + m, i);            }    n = 2 * m;}bool check(){    for(int i = 1; i <= m; i++)        if(fa[i] == fa[i + m]) return false;    return true;}void solve(){    for(int i = 1; i <= n; i++)        if(!dfn[i]) tarjan(i);    if(check()) printf("panda is telling the truth...\n");    else printf("the evil panda is lying again\n");}int main(){    scanf("%d%d", &n, &m);    init();    build();    solve();    return 0;}


0 0
原创粉丝点击