POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题)

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...

Http

POJ:https://vjudge.net/problem/POJ-3207

Source

2-sat

题目大意

给定一个圆及其上面的n个点,现在要连接上面的m对点,可以从圆外或圆内连接,要求不能相交。现在问这样的方案是否存在

解决思路

对于圆内和圆外我们可以把其看做两种状态,那么对于每一对点,我们把从圆外连接记作i,把从圆内连接记作i+m。

那如何连接边呢?我们知道在2-sat问题中若连接i->j则表示若取i则必取j,转换到这一题就是要找出那些会出现矛盾的点对,即两组点对不能同时从外面或同时从里面连接。

为了方便操作,我们在输入的时候就点对(x,y)x,y中较小的一个放到x中,大的放到y中,这样我们就可以把圆化成一个线段,在线段上处理了(为什么呢,仔细想一想)。

扫描所有点对,每次枚举点对i,j,那么出现矛盾会是什么情况呢?

当xj在xi与yi之间且yj不再xi与yi之间时,两组点对不能同时在圆外或圆内。所以可以以此建图。

因为这个题目只要判断可行性,所以在判断2-sat时既可以用在这一题中的dfs染色判断法,也可以用Tarjan缩点求强联通分量的方法。因为上一篇文章已经介绍过了染色法,加上这一题数据范围更大,所以这里介绍一下Tarjan法。

关于如何用Tarjan求强连通分量,这里不再重复(如果不知道,可以到这篇文章去看一看(施工中)),那么主要讲一讲为什么Tarjan缩点后就可以判断2-sat的可行性。

因为我们在2-sat建图时对于一条边i->j的意思是选i必须选j,而缩点后每一个强连通分量代表着互相可达,即表示若选择其中的一个则整个强联通分量中的点都必须选择。而若此时i与i+m在同一个强连通分量里,说明无解(一对点总不能既从圆外连,又从圆内连吧)。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<stack>
using namespace std; const int maxN=2001;
const int inf=2147483647; int n,m;
int Readx[maxN];
int Ready[maxN];
vector<int> E[maxN];//i与i+m为对应点
//Tarjan
int cnt=0;
int dfn[maxN];
int low[maxN];
bool instack[maxN];
stack<int> S;
//连通块(这里我就用拼音写法啦)
int LTKcnt=0;
int LTK[maxN]; void Read_and_Init();//读入并建图
void Link(int x,int y);//连接x与y
void tarjan(int u);
bool check(); int main()
{
Read_and_Init();
memset(instack,0,sizeof(instack));
memset(dfn,0,sizeof(dfn));
while (!S.empty())
S.pop();
for (int i=1;i<=2*m;i++)
if (dfn[i]==0)
tarjan(i);
if (check())
cout<<"panda is telling the truth..."<<endl;
else
cout<<"the evil panda is lying again"<<endl;
} void Read_and_Init()
{
cin>>n>>m;
int a,b;
for (int i=1;i<=m;i++)
{
cin>>Readx[i]>>Ready[i];
Readx[i]++;
Ready[i]++;
if (Readx[i]>Ready[i])//为了方便后面判断,将小的点放在前面
swap(Readx[i],Ready[i]);
}
for (int i=1;i<=m;i++)
for (int j=i+1;j<=m;j++)
{
if ( ((Readx[i]<=Readx[j])&&(Readx[j]<=Ready[i])&&(Ready[i]<=Ready[j]))
|| ((Readx[i]>=Readx[j])&&(Readx[i]<=Ready[j])&&(Ready[j]<=Ready[i])) )//这里即表示若i与j矛盾
{
Link(i,j+m);
Link(j,i+m);
Link(i+m,j);
Link(j+m,i);
}
}
return;
} void Link(int x,int y)
{
E[x].push_back(y);
return;
} void tarjan(int u)//Tarjan算法,不多说
{
cnt++;
dfn[u]=low[u]=cnt;
instack[u]=1;
S.push(u);
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i];
if (dfn[v]==0)
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
if (instack[v]==1)
low[u]=min(low[u],dfn[v]);
}
if (dfn[u]==low[u])
{
int v;
LTKcnt++;
do
{
v=S.top();
S.pop();
instack[v]=0;
LTK[v]=LTKcnt;
}
while (u!=v);
}
return;
} bool check()//检查是否有i与i+m在同一连通块
{
for (int i=1;i<=m;i++)
if (LTK[i]==LTK[i+m])
return 0;
return 1;
}

POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题)的更多相关文章

  1. poj 3207 Ikki's Story IV - Panda's Trick (2-SAT)

    http://poj.org/problem?id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 13 ...

  2. POJ 3207 Ikki's Story IV - Panda's Trick

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

  3. POJ 3207 Ikki's Story IV - Panda's Trick (2-sat)

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

  4. POJ 3207 Ikki's Story IV - Panda's Trick (2-SAT,基础)

    题意: 有一个环,环上n个点,现在在m个点对之间连一条线,线可以往圆外面绕,也可以往里面绕,问是否必定会相交? 思路: 根据所给的m条边可知,假设给的是a-b,那么a-b要么得绕环外,要么只能在环内, ...

  5. poj 3207 Ikki's Story IV - Panda's Trick【2-SAT+tarjan】

    注意到相交的点对一定要一里一外,这样就变成了2-SAT模型 然后我建边的时候石乐志,实际上不需要考虑这个点对的边是正着连还是反着连,因为不管怎么连,能相交的总会相交,所以直接判相交即可 然后tarja ...

  6. POJ 3207 Ikki's Story IV - Panda's Trick 2-sat模板题

    题意: 平面上,一个圆,圆的边上按顺时针放着n个点.现在要连m条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接.给你的信息中,每个点最多只会连接的一条边.问能不能连接这m条边,使这 ...

  7. 【POJ】3207 Ikki's Story IV - Panda's Trick

    http://poj.org/problem?id=3207 题意:一个圆上顺时针依次排列着标号为1-n的点,这些点之间共有m条边相连,每两个点只能在圆内或者圆外连边.问是否存在这些边不相交的方案.( ...

  8. 【POJ3207】Ikki's Story IV - Panda's Trick

    POJ 3207 Ikki's Story IV - Panda's Trick liympanda, one of Ikki's friend, likes playing games with I ...

  9. Ikki's Story IV - Panda's Trick POJ - 3207(水2 - sat 在圈内 还是 在圈外)

    题意: 就是一个圈上有n个点,给出m对个点,这m对个点,每一对都有一条边,合理安排这些边在圈内或圈外,能否不相交 解析: 我手残 我手残 我手残 写一下情况 只能是一个在圈外 一个在圈内 即一个1一个 ...

随机推荐

  1. Vulkan Tutorial 08 交换链

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 在这一章节,我们了解一下将渲染图像提交到屏幕的基本机制.这种机制成为交换链,并且需要 ...

  2. 从零开始的JS生活(三)——内置对象

    咱们继续进行我们的正经的JS介绍.今天所要跟大家讲述的是JS中十分常用.十分常用.十分常用的内置对象. 一.世界上最熟悉的陌生就是,当你看着数组.而我看着你... - 数组对象 1.数组的概念 在内存 ...

  3. 低版本eclipse导入高版本eclipse创建项目报错问题

    例如用高版本eclipse创建的项目,会默认使用的是jdk1.8版本, 低版本eclipse创建项目,会默认使用的是jdk1.7版本. 此时导入高版本eclipse项目时会报错(文件夹中会出现红色!) ...

  4. TreeSet集合排序方式二:定制排序Comparator

    Comparator有两种实现方式: 1,匿名内部类 2,创建一个类用于实现Comparator,该类创建的对象就是比较器 Person类 public class Person implements ...

  5. 抓包工具 - HttpWatch

    HttpWatch是功能强大的网页数据分析工具,集成在IE工具栏,主要功能有网页摘要.cookies管理.缓存管理.消息头发送/接收,字符查询.POST数据.目录管理功能和报告输出.HttpWatch ...

  6. Gradle入门学习---认识buildeTypes和dependencies

    Gradle是Android Studio默认的构建工具,如果是基本的APP开发,不会涉及到Gradle太多内容,毕竟它的诞生就不是专为Android服务的. 日常开发需要涉及到使用Gradle的场景 ...

  7. php注册登录源代码

    php注册登录源代码 链接数据库<?php$conn=mysql_connect('localhost','root','');mysql_select_db('ht',$conn);mysql ...

  8. vue-schart : vue.js 的图表组件

    介绍 vue-schart 是使用vue.js封装了sChart.js图表库的一个小组件.支持vue.js 1.x & 2.x 仓库地址:https://github.com/lin-xin/ ...

  9. python基础之字典dict和集合set

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7043642.html python基础之字典dict和集合set 字典dic ...

  10. 使用requireJs的方法

    在你们对requireJs初步了解后,快来看看他们是怎么使用的吧. 在你下载完成require.js插件后,在页面里引入,在require.js 加载完之后,会查找页面上script标签的data-m ...