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. C++ STL快速入门

    在数月之前的机试中第一次体验到STL的威力,因为自己本来一直在用C语言做开发,很多数据结构都是自己造的,比如链表.队列等,第一次接触C++ STL后发现这些数据结构都已经给我提供好了,我直接拿去调用就 ...

  2. setTimeout异步加载

    两道经典的面试题,直接上代码 for(var i=0; i<3; i++){ setTimeout(function(){ i+=i; console.log(i); },1000) } var ...

  3. 网络编程4之UDP协议

    一.定义 UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种[无 ...

  4. BUGKUctf-web-writeup

    ---恢复内容开始--- 找到了个ctf平台.里面的web挺多的.终于将web题目写的差不多了. Web 签到题 加群就可以了 Web2 直接F12就看到了 文件上传测试 Burp抓包 文件名改成 1 ...

  5. python基础操作

    1.打印操作 print('2222') 2.接收用户输入 name=input('name') 3.if else判断 name='qiao'name2='师弟'username=input('输入 ...

  6. 50几个photoshop快捷键

    一.常用的热键组合 1.图层混合模式快捷键:正常(Shift + Option + N),正片叠底(Shift + Option + M),滤色(Shift + Option + S),叠加(Shif ...

  7. OSS web直传 ajax方式 上传图片、文件

    部分js代码 send_request = function(){//这是从后台获取认证策略等信息. var htmlobj=$.ajax({url:root+"/service/polic ...

  8. Vue基础概念,学习环境等

    前提: 你已有 HTML.CSS 和 JavaScript 中级前端知识. 概念: Vue.js(读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vu ...

  9. robot framework环境搭建

    来源:http://www.cnblogs.com/puresoul/p/3854963.html[转] 一. robot framework环境搭建: 官网:http://robotframewor ...

  10. 常用linux小工具介绍

    1.ctags(Generate tag files for source code)是vim下方便代码阅读的工具.尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM. ctags 最先是 ...