poj3207(two-sat)
传送门:Ikki's Story IV - Panda's Trick
题意:给定一个圆,圆上一些点。两点一线。现给出一些线,这些线可以在圆内连起来,也可以在圆外。问有没有可能所有的线画完且不出现相交。
分析:对于每条线,要么在圆外,要么在圆内,且不可同时满足,只能两者取一,判断这M条线是否合法,也就是M条线不冲突,这就是典型的2-sat问题了。
将每条线在圆内当成一点i,在圆外当成一点i',对于两条线,如果在园内同时在圆内,那么必定也不能同时在园外,则有边(i, j') 、(j ,i')、(i',j)、(j' ,i)。
这题由于不用输出方案,直接tarjan缩点后判断[i,i']是否属于一个强连通内即可。
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 2010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
int v,next;
edge(){}
edge(int v,int next):v(v),next(next){}
}e[N*N/];
struct node
{
int l,r;
}s[N];
int n,m,scc,step,top,tot;
int head[N],dfn[N],low[N],belong[N],Stack[N];
bool instack[N];
void init()
{
tot=;step=;scc=;top=;
FILL(head,-);FILL(dfn,);
FILL(low,);FILL(instack,false);
}
void addedge(int u,int v)
{
e[tot]=edge(v,head[u]);
head[u]=tot++;
}
void tarjan(int u)
{
int v;
dfn[u]=low[u]=++step;
Stack[top++]=u;
instack[u]=true;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
scc++;
do
{
v=Stack[--top];
instack[v]=false;
belong[v]=scc;
}while(v!=u);
}
}
bool ok(int i,int j)
{
int x1=s[i].l,y1=s[i].r;
int x2=s[j].l,y2=s[j].r;
if( x2>x1&&x2<y1 ){
if( y2>=y1 ) return true;
if( y2<=x1 ) return true;
}
if( y2>x1&&y2<y1 ){
if( x2>=y1 ) return true;
if( x2<=x1 ) return true;
}
return false;
}
void solve()
{
for(int i=;i<=*m;i++)
if(!dfn[i])tarjan(i);
bool flag=true;
for(int i=;i<=m;i++)
{
if(belong[i]==belong[i+m])
{
flag=false;
break;
}
}
if(flag)puts("panda is telling the truth...");
else puts("the evil panda is lying again");
}
int main()
{
int a,b;
while(scanf("%d%d",&n,&m)>)
{
init();
for(int i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
a++;b++;
s[i].l=min(a,b);
s[i].r=max(a,b);
}
for(int i=;i<=m;i++)
for(int j=i+;j<=m;j++)
if(ok(i,j))
{
addedge(i,j+m);
addedge(j+m,i);
addedge(j,i+m);
addedge(i+m,j);
}
solve();
}
}
poj3207(two-sat)的更多相关文章
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
- Katu Puzzle POJ - 3678 (2 - sat)
有N个变量X1X1~XNXN,每个变量的可能取值为0或1. 给定M个算式,每个算式形如 XaopXb=cXaopXb=c,其中 a,b 是变量编号,c 是数字0或1,op 是 and,or,xor 三 ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )
在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.or ...
- MongoDB 聚合管道(Aggregation Pipeline)
管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...
- mysql触发器,答题记录表同步教学跟踪(用户列表)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVQAAAOOCAIAAABgEw4AAAAgAElEQVR4nOy92VcT27r/zX+xLtflvt
- Linux版Matlab R2015b的bug——脚本运行的陷阱(未解决)
0 系统+软件版本 系统:CentOS 6.7 x64, 内核 2.6.32-573.el6.x86_64软件:Matlab R2015b(包括威锋网和东北大学ipv6下载的资源,都测试过) 1 脚本 ...
- Quartz.net(调度框架) 使用Mysql作为存储
最近公司的做的项目中涉及到配置任务地址然后按照配置去目标地址提取相关的数据,所以今天上午在Internet上查看有关定时任务(调度任务)的相关信息,筛选半天然后查找到Quartz.net. Quart ...
- mysql 函数编程大全(持续更新)
insert ignore insert ignore表示,如果中已经存在相同的记录,则忽略当前新数据 如果您使用一个例如“SET col_name = col_name + 1”的赋值,则对位于右侧 ...
随机推荐
- vim的ex模式用法
本文出自 http://blog.csdn.net/shuangde800 本文是在学习<使用vi编辑器, Lamb & Robbins编著>时在evernote写的其中一章笔 ...
- spark中各种连接操作以及有用方法
val a = sc.parallelize(Array(("123",4.0),("456",9.0),("789",9.0)) val ...
- 【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])
需求,要求批量新增或者改动一个List,在Spring MVC中是不支持以下代码的写法 @RequestMapping(value = "/update", method = Re ...
- paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制
源地址:http://blog.csdn.net/attilax/article/details/12343625 paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制 效果图: ...
- 鼠标进入与离开的消息(使用CM_MOUSEENTER来判断是否进入控件)
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs ...
- Flex上传文件
前几天写了一篇jsp页面利用ajaxFileUpload上传文件.如今把flex上传页面也分享出来: 前台页面 <?xml version="1.0" encoding=&q ...
- delpi中的RTTI初试
java中的反射机制使我们能够在运行期间获取运行期类的信息,那么在delphi中有没有这样的功能呢?答案是有,实现这种功能的机制在delphi中叫做RTTI,废话少说,先来一段demo: 1.先定义一 ...
- linux:sed高级命令之n、N(转)
sed的语法格式: sed [option] {sed-command} {input-file} sed在正常情况下,将处理的行读入模式空间(pattern space),脚本中的“sed-comm ...
- haproxy redirect location和redirect prefix
<pre name="code" class="html">redirect location <loc> [code <code ...
- 《Java从入门到精通》src0-8
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello wo ...