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. 如何有效的跟踪线上 MySQL 实例表和权限的变更

    介绍 从系统管理员或 DBA 的角度来讲, 总期望将线上的各种变更限制在一个可控的范围内, 减少一些不确定的因素. 这样做有几点好处: . 记录线上的库表变更; . 对线上的库表变更有全局的了解; . ...

  2. 利用nodeJs来安装less以及编译less文件为css文件

    NodeJs 使用nodejs安装less以及编译less文件为css文件 首先下载nodeJs的安装包,按照步骤,安装nodejs. 链接:http://pan.baidu.com/s/1dEsqY ...

  3. 生成JSON数据--官方方法

    官方生成方法: 1)需要什么就给什么,要属性就给属性,要对象就给对象,要集合就给集合 2)添加都是使用put()方法 要求: 1.生成如下JSON数据: {"age":4,&quo ...

  4. angularJS ng-change错误的解决方案

    导入文件:<script src="../../js/angular/angular-file-upload/angular-file-upload.js"></ ...

  5. Spring学习(24)--- AOP之 Aspect instantiation models(aspect实例模式)特别说明

    重要: schema-defined aspects只支持singleton model,即 基于配置文件的aspects只支持单例模式

  6. SQL Server Alwayson读写分离配置

    标签:MSSQL/只读路由 概述 Alwayson相对于数据库镜像最大的优势就是可读副本,带来可读副本的同时还添加了一个新的功能就是配置只读路由实现读写分离:当然这里的读写分离稍微夸张了一点,只能称之 ...

  7. 记一次 net 使用 data.oracleclient 使用错误

    前提: 公司除了领导和开发人员具有管理员权限,其他人员使用的都是域账号. 过程: 应要求开发一个 winfrom项目,使用data.oracleclient  本地开发,调试无误,放到服务器共享域用户 ...

  8. python爬虫之re正则表达式库

    python爬虫之re正则表达式库 正则表达式是用来简洁表达一组字符串的表达式. 编译:将符合正则表达式语法的字符串转换成正则表达式特征 操作符 说明 实例 . 表示任何单个字符 [ ] 字符集,对单 ...

  9. Error creating bean with name 'signController': Injection of autowired dependencies failed

    出现了一大串错误,Error creating bean with name 'signController': Injection of autowired dependencies failed. ...

  10. Dom 简介

    HTML DOM 简介 DOM 教程 DOM 节点 HTML DOM 定义了访问和操作 HTML 文档的标准. 您应该具备的基础知识 在您继续学习之前,您需要对以下内容拥有基本的了解: HTML CS ...