POJ3207Ikki's Story IV - Panda's Trick(模板题)
题意:平面上,一个圆,圆的边上按顺时针放着n个点。现在要连m条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接。给你的信息中,每个点最多只会连接的一条边。问能不能连接这m条边,使这些边都不相交。
分析:将每一条边缩成一个点,对于每条边要么在圆内要么在园外只有满足一种情况,将在圆内设为i,在圆外设为i',那么对于两条线(i, j)不能同时在圆内,那么也不能同时在圆外。则有四种情况 (i, j') (i', j) (j, i') (j', i)要建这四条边,然后就照着刘汝佳书上的抄了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <stack>
#include <algorithm>
using namespace std;
const int Max = ;
struct TWOSAT
{
int n;
vector<int> G[Max * ];
bool mark[Max * ];
int S[Max * ], c;
void init(int n)
{
this->n = n;
for (int i = ; i <= n * ; i++)
G[i].clear();
memset(mark, , sizeof(mark));
}
void add_clause(int x, int y)
{
G[ * x].push_back( * y + ); //四条边
G[ * x + ].push_back( * y);
G[ * y + ].push_back( * x);
G[ * y].push_back( * x + );
}
bool dfs(int x)
{
if (mark[x ^ ])
return false;
if (mark[x])
return true;
mark[x] = true;
S[c++] = x;
for (int i = ; i < (int) G[x].size(); i++)
if (!dfs(G[x][i]))
return false;
return true;
}
bool solve()
{
for (int i = ; i <= n * ; i += )
{
if (!mark[i] && !mark[i + ])
{
c = ;
if (!dfs(i))
{
while (c > )
mark[ S[--c] ] = false;
if (!dfs(i + ))
return false;
}
}
}
return true;
}
};
struct Node
{
int s, t;
}node[Max];
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = ; i <= m; i++)
{
scanf("%d%d", &node[i].s, &node[i].t);
if (node[i].s > node[i].t) //这一步让判断相交很方便
swap(node[i].s, node[i].t);
}
TWOSAT twosat;
twosat.init(m);
for (int i = ; i <= m; i++)
{
for (int j = i + ; j <= m; j++)
{
if ( (node[i].s < node[j].s && node[i].t < node[j].t && node[i].t > node[j].s) || (node[j].s < node[i].s && node[i].t > node[j].t && node[j].t > node[i].s) ) //相交的情况, i 的起点 在 j 的 起点和终点之间并且i的终点要大于j的终点, 另一种情况类似
{
twosat.add_clause(i, j);
}
}
}
if (twosat.solve())
printf("panda is telling the truth...\n");
else
printf("the evil panda is lying again\n");
}
}
POJ3207Ikki's Story IV - Panda's Trick(模板题)的更多相关文章
- poj--3207--Ikki's Story IV - Panda's Trick(2-sat)
Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %I64d ...
- 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)
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(2-sat问题)
POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题) Description liympanda, one of Ikki's friend, likes ...
- 【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 ...
- 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条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接.给你的信息中,每个点最多只会连接的一条边.问能不能连接这m条边,使这 ...
- poj3207 Ikki's Story IV - Panda's Trick 2-SAT
题目传送门 题意:在一个圆上顺时针安放着n个点,给出m条线段连接端点,要求线段不相交,线段可以在圆内也可以在圆外,问是否可以. 思路:假设一条线段,放在圆外是A,放在园内是A',那么两条线段如果必须一 ...
- 【POJ】3207 Ikki's Story IV - Panda's Trick
http://poj.org/problem?id=3207 题意:一个圆上顺时针依次排列着标号为1-n的点,这些点之间共有m条边相连,每两个点只能在圆内或者圆外连边.问是否存在这些边不相交的方案.( ...
随机推荐
- Linux sysinfo获取系统相关信息
Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...
- Ext.Net-Grid 篇
概述 前两篇分别介绍了Ext.NET-基础 和 Ext.NET-布局,从本篇开始我们尽量做一些实际工作中用到的例子. 在Ext.NET官方示例中,关于GridPanel的例子是最多的(近百个),篇幅所 ...
- tr命令
tr命令是linux下一个字符处理命令,用途: 字符替换 字符删除 字符压缩形式:tr [OPTION]... SET1 [SET2]接口:输入输出都是标准流,所以要通过管道来调用这 ...
- linux下PHP7环境搭建
LAMP环境版本 操作系统:Centos 7 Mysql:5.7.11 Apache:2.4.18 PHP:7.0.4 安装Mysql 下载链接:http://dev.mysql.com/ ...
- difference between append and appendTo
if you need append some string to element and need set some attribute on these string at the same ti ...
- jQuery自定义插件
jQuery自定义插件 jQuery自定义插件按照功能分类,可以分为三类, 1>封装对象方法的插件,(也就是基于某个DOM元素的jQuery对象,局部的) 2>封装全局函数的插件, ( ...
- Linux14.04安装Mysql Linux公社
今天在Ubuntu 14.04下安装MySQL,本来是去官网下载安装包来安装的,可是安装之后却不能用,估计是要配置吧,在网上搜了很多的资料,结果还是失败.所以只好在软件源中安装,这样就省去很多不必要的 ...
- [转]Java中怎样判断一个字符串能否转成数字
原文地址:http://blog.sina.com.cn/s/blog_7bac470701014mjf.html 判断字符串是否为数字 //1.正则表达式 public static boolea ...
- flex布局无法自动适应的bug以及实现textarea根据内容自适应
-webkit-box布局无法自动适应高度的bug css3的新属性display:-webkit-box带来了前端开发自动适应布局的春天 ,但是我发现这个布局有个问题, 而且这个问题我无法解决: 描 ...
- java-byte[]图片在页面展示
public void img(HttpServletRequest req, HttpServletResponse res) { //res.setHeader("Content-Typ ...