POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题)
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问题)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- POJ 3207 Ikki's Story IV - Panda's Trick (2-SAT,基础)
题意: 有一个环,环上n个点,现在在m个点对之间连一条线,线可以往圆外面绕,也可以往里面绕,问是否必定会相交? 思路: 根据所给的m条边可知,假设给的是a-b,那么a-b要么得绕环外,要么只能在环内, ...
- poj 3207 Ikki's Story IV - Panda's Trick【2-SAT+tarjan】
注意到相交的点对一定要一里一外,这样就变成了2-SAT模型 然后我建边的时候石乐志,实际上不需要考虑这个点对的边是正着连还是反着连,因为不管怎么连,能相交的总会相交,所以直接判相交即可 然后tarja ...
- POJ 3207 Ikki's Story IV - Panda's Trick 2-sat模板题
题意: 平面上,一个圆,圆的边上按顺时针放着n个点.现在要连m条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接.给你的信息中,每个点最多只会连接的一条边.问能不能连接这m条边,使这 ...
- 【POJ】3207 Ikki's Story IV - Panda's Trick
http://poj.org/problem?id=3207 题意:一个圆上顺时针依次排列着标号为1-n的点,这些点之间共有m条边相连,每两个点只能在圆内或者圆外连边.问是否存在这些边不相交的方案.( ...
- 【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 ...
- Ikki's Story IV - Panda's Trick POJ - 3207(水2 - sat 在圈内 还是 在圈外)
题意: 就是一个圈上有n个点,给出m对个点,这m对个点,每一对都有一条边,合理安排这些边在圈内或圈外,能否不相交 解析: 我手残 我手残 我手残 写一下情况 只能是一个在圈外 一个在圈内 即一个1一个 ...
随机推荐
- String为值类型还是引用类型
关于String为值类型还是引用类型的讨论一直没有平息,最近一直在研究性能方面的问题,今天再次将此问题进行一次明确.希望能给大家带来点帮助. 如果有错误请指出. 来看下面例子: //值类型 int a ...
- IE 不兼容 js indexOf 函数
在使用 js 判断数组中是否存储该元素,我们会用到 indexOf 函数.而在 IE 上 indexOf 函数 无法兼容,通过以下方法解决,仅以文章记录一下 if (!Array.prototyp ...
- Java中IO流的总结
有关Java中IO流总结图 流分类 按方向分 输入流 输出流 按单位分 字节流 字符流 按功能分 节点流 处理流(过滤流) 其他 所有的流继承与这四类流:InputSteam.OutputStream ...
- socket聊天室(服务端)(多线程)(TCP)
#include<string.h> #include<signal.h> #include<stdio.h> #include<sys/socket.h&g ...
- Core ML 机器学习
在WWDC 2017开发者大会上,苹果宣布了一系列新的面向开发者的机器学习 API,包括面部识别的视觉 API.自然语言处理 API,这些 API 集成了苹果所谓的 Core ML 框架.Core M ...
- Webpack 资源管理
Webpack 资源管理
- vue 高德地图之玩转周边
前言:在之前的博客中,有成功引入高德地图,这是以前的地址 vue 调用高德地图. 因为一些需求,需要使用到地图的周边功能. 完整的项目代码请查看 我的github 一 .先看要实现的结果,参考了链 ...
- C#与Java对比学习
Eclipse开发环境与VS开发环境的调试对比 数据类型.集合类.栈与队列.迭达.可变参数.枚举 类型判断.类与接口继承.代码规范与编码习惯.常量定义
- Solr集群搭建
SolrCloud需要solr基于zookeeper部署,zookeeper是一个集群管理软件,由于SolrCloud需要由多台服务器组成.由zookeeper来进行协调管理.Zookeeper是一个 ...
- loadrunner11的移动端性能测试之脚本录制
以前使用LR11录制过一个app的登录操作,这里记录一下 测试准备 硬件要求 1, 负载测试机一台(内存8G以上,cpu 1.5GHZ以上,存储空间20G以上)或两台以上(控制机和多个负载机). 2 ...