POJ 3207 Ikki's Story IV - Panda's Trick 【经典2-SAT】


传送门:POJ 3207


Ikki's Story IV - Panda's Trick
Time Limit: 1000MS Memory Limit: 131072K

Problem 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…



题意:
出一个圆上的 n 个点(0~n-1编号),然后在指定的 m 对点之间各连一条线(可以在圆内,也可以在圆外,可以是曲线),然后问你是否能使这些线都不相交。
在这里插入图片描述
题意有点难懂,看 oj 上的discuss上一个人的解释才看懂。


题解:
对于每一条边都有两个状态:连线在圆内和连线在圆外。再加上有m个边,可以看成是2-sat的模子。
那么题目的限制条件是线不相交,所以我们考虑圆上两线相交的条件。

假设有两条边i,j,第一条边两个端点为a1,b1,第二条边为a2,b2;
我们可以看成如果 a 1 &lt; a 2 且 b 1 &lt; b 2 且 b 1 &gt; a 2 a1&lt;a2且b1&lt;b2且b1&gt;a2 a1<a2b1<b2b1>a2 的话两条线在圆的同一侧是一定相交的。
那么我们对于这两条边就可以这样建2-sat的图:
先将m条边按 a a a 有小到大排序
i i i 表示第 i 条边连在圆内, i ′ i&#x27; i 表示第 j 条边连在圆外。
那么建边就应该是:
i i i - > j ′ j&#x27; j i ′ i&#x27; i - > j j j j j j - > i ′ i&#x27; i j ′ j&#x27; j - > i i i
( i i i - > j ′ j&#x27; j表示边 i 连在圆内,边 j 就得连在圆外。其他的同理)。

建完图后,我们只要用2-sat跑一下判断可行性就行。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=1100;
int n,m,top;
bool mark[2*N];
int stack[2*N];
vector<int> g[2*N];
struct Node
{
  int a,b;
}edges[N];
bool cmp(Node x,Node y)
{
  return x.a<y.a;
}
void init()
{
  memset(mark,false,sizeof(mark));
  for(int i=0;i<2*N;i++)
  {
    g[i].clear();
  }
}
void addedge(int x,int idx,int y,int idy)
{
  x=2*x+idx;
  y=2*y+idy;
  g[x].push_back(y);
}
bool dfs(int x)
{
  if(mark[x^1]) return false;
  if(mark[x]) return true;
  mark[x]=true;
  stack[++top]=x;
  int len=g[x].size();
  for(int i=0;i<len;i++)
  {
    if(!dfs(g[x][i])) return false;
  }
  return true;
}
bool twosat()
{
  for(int i=0;i<2*n;i+=2)
  {
    if(!mark[i]&&!mark[i+1])
    {
      top=0;
      if(!dfs(i))
      {
        while(top>0)
          mark[stack[top--]]=false;
        if(!dfs(i+1)) return false;
      }
    }
  }
  return true;
}
int main()
{
  while(scanf("%d%d",&n,&m)!=EOF)
  {
    init();
    for(int i=0;i<m;i++)
    {
      scanf("%d%d",&edges[i].a,&edges[i].b);
      if(edges[i].a>edges[i].b)
      {
        int tmp=edges[i].a;
        edges[i].a=edges[i].b;
        edges[i].b=tmp;
      }
    }
    sort(edges,edges+m,cmp);
    for(int i=0;i<m;i++)
    {
      for(int j=i+1;j<m;j++)
      {
        if(edges[j].b>edges[i].b&&edges[i].b>edges[j].a)
        {
          addedge(i,0,j,1);
          addedge(i,1,j,0);
          addedge(j,0,i,1);
          addedge(j,1,i,0);
        }
      }
    }
    if(twosat()) printf("panda is telling the truth...\n");
    else printf("the evil panda is lying again\n");
  }
  return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值