poj3207-Ikki's Story IV - Panda's Trick

poj3207-Ikki’s Story IV - Panda’s Trick

Ikki’s Story IV - Panda’s Trick
Time Limit: 1000MS
Memory Limit: 131072K
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
POJ Monthly–2007.03.04, Ikki

2-SET问题。设(p1,p2)为圆上两点在圆内部连线和外部连线的两种可能,p1,p2只能二选一。(q1,q2)同理。显然,如果p1,q1相交,则p2,q2相交。如果p1,q1相交,那么我们在(p1,q2)和(p2,q1)间建一条边。求出图中的强连通分量。如果(p1, p2)或(q1,q2)在同一个集合里,那么无解,否则有解。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 1000000 + 10;

struct twoSet{
    int tot, color, m;
    int vis[MAXN], end[MAXN], col[MAXN];
    vector<int> map[MAXN];

    void addEdge(int u, int v) {
      map[u].push_back(v);
      map[v].push_back(u);
    }

    void dfs1(int u) {
      vis[u] = 1;
      for (int i = 0; i < map[u].size(); ++i) {
        int v = map[u][i];
        if (vis[v]) continue;
        dfs1(v);
      }
      end[++tot] = u;
    }

    void dfs2(int u) {
      col[u] = color;
      for (int i = 0; i < map[u].size(); ++i) {
        int v = map[u][i];
        if (col[v]) continue;
        dfs2(v);
      }
    }

    bool solve(){
      memset(vis, 0, sizeof vis);
      memset(col, 0, sizeof col);
      memset(end, 0, sizeof end);
      tot = 0;
      for (int i = 1; i <= m*2; ++i)
        if (!vis[i])
          dfs1(i);
      color = 0;
      for (int i = 1; i <= m*2; ++i)
        if (!col[end[i]]) {
          ++color;
          dfs2(end[i]);
        }
      for (int i = 1; i <= m; ++i)
        if (col[i] == col[i+m])
          return 0;
      return 1;
    }
}tS;

pair<int, int> p[MAXN];
#define x first
#define y second

int main()
{
    int n, m, x, y, tot = 0;
    scanf("%d%d", &n, &m);tS.m = m;
    for (int i = 1; i <= m; ++i) {
      scanf("%d%d", &p[i].x, &p[i].y);
      if (p[i].x > p[i].y) swap(p[i].x, p[i].y); 
    }
    for (int i = 1; i < m; i++)
      for (int j = i + 1; j <= m; j++) 
        if ((p[i].x < p[j].x && p[i].y < p[j].y && p[j].y < p[i].y) || 
            (p[i].x > p[j].x && p[i].y > p[j].y && p[i].y < p[j].y) || 
            (p[i].x < p[j].x && p[i].y > p[j].x && p[i].y < p[j].y) ||
            (p[j].x < p[i].x && p[j].y > p[i].x && p[j].y < p[i].y)) {
          tS.addEdge(i, j+m);
          tS.addEdge(i+m, j);
        }
    if (tS.solve()) printf("panda is telling the truth...");
    else printf("the evil panda is lying again");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值