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...
题意:有一个圆,给出一些边连接着两个点,边可以从圆里连,也可以从圆外连,问是否可以不相交
对于边i,j限制条件为不相交,即不在同一个集合中
因此我们将这个问题转化为了2-SAT问题
设i表示边i在圆内,i'表示i在圆外
若(i,j)在圆内相交,那么它们在圆外也一定相交
如果边i,j在圆内相交
那么就从i连向j'(i内j外),从j'连向i(i内j外),从j连向i'(j内i外),从i'连向j(j内i外)
然后来一遍tarjan就好了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#define Pair pair<int,int>
#define F first
#define S second
using namespace std;
const int MAXN=1e6+10;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
char buf[1<<20],*p1=buf,*p2=buf;
inline int read()
{ char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
Pair p[MAXN];
struct node
{
int u,v,nxt;
}edge[MAXN];
int head[MAXN],num=1;
inline void AddEdge(int x,int y)
{
edge[num].u=x;
edge[num].v=y;
edge[num].nxt=head[x];
head[x]=num++;
}
int dfn[MAXN],low[MAXN],vis[MAXN],color[MAXN],colornum=0,tot=0;
stack<int>s;
void tarjan(int now)
{
dfn[now]=low[now]=++tot;
s.push(now);
vis[now]=1;
for(int i=head[now];i!=-1;i=edge[i].nxt)
{
if(!dfn[edge[i].v])
tarjan(edge[i].v),low[now]=min(low[now],low[edge[i].v]);
if(vis[edge[i].v]) low[now]=min(low[now],dfn[edge[i].v]);
}
if(dfn[now]==low[now])
{
colornum++;
int h=0;
do
{
h=s.top();s.pop();
vis[h]=0;
color[h]=colornum;
}while(h!=now);
}
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
memset(head,-1,sizeof(head));
int N=read(),M=read();
for(int i=1;i<=M;i++)
{
p[i].F=read(),p[i].S=read();
if(p[i].F>p[i].S) swap(p[i].F,p[i].S);
}
for(int i=1;i<=M;i++)
{
for(int j=i+1;j<=M;j++)
{
if((p[j].F>=p[i].F&&p[j].F<=p[i].S&&p[j].S>=p[i].S)||
(p[j].F<=p[i].F&&p[j].S>=p[i].F&&p[j].S<=p[i].S))
AddEdge(i,j+M),
AddEdge(j,i+M),
AddEdge(j+M,i),
AddEdge(i+M,j);
}
}
for(int i=1;i<=M;i++)
if(!dfn[i])
tarjan(i);
bool flag=1;
for(int i=1;i<=M;i++)
if(color[i]==color[i+M])
{printf("the evil panda is lying againn");flag=0;break;}
if(flag==1) printf("panda is telling the truth...n");
return 0;
}
- 使用 Vagrant 打造跨平台开发环境
- HDU 2084 数塔(简单DP入门)
- Python-解决Cx_Oracle查询时UnicodeDecodeError的问题
- “玲珑杯”ACM比赛 Round #12题解&源码
- NYOJ 题目77 开灯问题(简单模拟)
- python笔记2-冒泡排序
- Lucas定理学习(进阶中)
- Java8时间类使用方法
- Selenium2+python自动化46-js解决click失效问题
- PCA实现一个简单的酒店推荐系统(附Python源码)
- 【干货】动手实践:理解和优化GAN(附代码)
- Selenium2+python自动化44-元素定位参数化(find_element)
- Selenium2+python自动化45-18种定位方法(find_elements)
- Python做文本挖掘的情感极性分析(基于情感词典的方法)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- pytorch 计算ConvTranspose1d输出特征大小方式
- Keras中 ImageDataGenerator函数的参数用法
- CI框架网页缓存简单用法分析
- 掌握PHP垃圾回收机制详解
- PHP基于面向对象封装的分页类示例
- PHP获取对象属性的三种方法实例分析
- PHP7内核之Reference详解
- PHP基于PDO扩展操作mysql数据库示例
- php实现的PDO异常处理操作分析
- PHP yield关键字功能与用法分析
- php PDO属性设置与操作方法分析
- PHP时间日期增减操作示例【date strtotime实现加一天、加一月等操作】
- CI框架实现创建自定义类库的方法
- php如何计算两坐标点之间的距离
- Python调用C语言程序方法解析