POJ 3207|Ikki's Story IV - Panda's Trick|2-SAT

11 篇文章 0 订阅

题目大意

在圆上连m条边,可以在圆内或圆外,问是否存在可行方案使边不相交。

题解

讨论边之间的关系,为每条边建立2个节点,分别表示在圆内和圆外。显然通过边的端点易知是否必须两侧,如果是,那么添加限制,即分别连接两条边的圆内和圆外的表示点。如果存在某条边的两个点联通,那么就不存在可行方案。

#include <cstdio>
#include <algorithm>
using namespace std;
#define FOR(i,j,k) for(i=j;i<=k;++i)
const int N = 1005, M = 600000;
int h[N], p[M], v[M], x[N], y[N], cnt = 0;
int dfn[N], low[N], belong[N], ts = 0, top = 0, scc = 0;
int stack[N], instack[N];
void add(int a, int b) {
    p[++cnt] = h[a]; v[cnt] = b; h[a] = cnt;
}

void tarjan(int x) {
    int i;
    dfn[x] = low[x] = ++ts;
    instack[x] = 1; stack[++top] = x;
    for (i = h[x]; i; i = p[i])
        if (!dfn[v[i]]) {
            tarjan(v[i]);
            low[x] = min(low[v[i]], low[x]);
        } else if (instack[v[i]])
            low[x] = min(dfn[v[i]], low[x]);
    if (dfn[x] == low[x]) {
        ++scc;
        do {
            i = stack[top--];
            instack[i] = 0;
            belong[i] = scc;
        } while (i != x);
    }
}

int main() {
    int n, m, i, j;
    scanf("%d%d", &n, &m);
    FOR(i,1,m) {
        scanf("%d%d", x + i, y + i);
        ++x[i]; ++y[i];
        if (x[i] > y[i]) swap(x[i], y[i]);
    }
    FOR(i,1,m) FOR(j,i+1,m)
        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]) {
            add(i, j + m); add(j, i + m);
            add(j + m, j); add(j + m, i);
        }
    FOR(i,1,2*m) if (!dfn[i]) tarjan(i);
    FOR(i,1,m) if (belong[i] == belong[i + m])
        return puts("the evil panda is lying again");
    return puts("panda is telling the truth...");
}

Ikki’s Story IV - Panda’s Trick

Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 9184 Accepted: 3391

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...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值