【poj3207】Ikki's Story IV - Panda's Trick(2-sat)
题意:
给出一个圆,圆上有\(n\)个点,依次为\(0,1,\cdots,n-1\)。
现在要连接\(m\)对点,每次连接时可以直接从里面连,也可以从外面连。
最后问,连完这\(m\)对点后,是否有线相交。
思路:
每次连接时可以直接从里面连,也可以从外面连,那么可以考虑这个问题是一个\(2-sat\)模型。\(2-sat\)模型一般可以表示为:\((x_1\bigvee x_2)\bigwedge (x_3\bigvee x_4)\bigwedge \cdots\)。大概就是这样,每个括号里面有两个变量。
那么我们现在将矛盾找出来,这个在纸上画一下就比较好找了。
最后的二元关系是:两个变量\(x_i,x_j\)只能选一个,也就是说一个往里面连,另一个往外面连。
所以我们连边\(x_i'\rightarrow x_j,x_j'\rightarrow x_i,x_i\rightarrow x_j',x_j\rightarrow x_i'\)即可,表示两者选择一个。
感觉这个题数据好水,后面做一个类似的题的时候代码被hack了。
/*
* Author: heyuhhh
* Created Time: 2019/11/29 15:38:08
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <cstring>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2005;
int n, m;
int a[N], b[N];
vector<int> G[N], rG[N], vs;
int used[N], bel[N];
void adde(int from, int to) {
G[from].push_back(to);
rG[to].push_back(from);
}
void dfs(int v) {
used[v] = true;
for(int i = 0; i < sz(G[v]); i++) {
int u = G[v][i];
if(!used[u])
dfs(u);
}
vs.push_back(v);
}
void rdfs(int v, int k) {
used[v] = true;
bel[v] = k;
for(int i = 0; i < sz(G[v]); i++) {
int u = G[v][i];
if(!used[u])
rdfs(u, k);
}
}
int scc() {
memset(used, 0, sizeof(used));
vs.clear();
for(int v = 1; v <= 2 * m; ++v)
if(!used[v]) dfs(v);
memset(used, 0, sizeof(used));
int k = 0;
for(int i = (int) vs.size() - 1; i >= 0; --i)
if(!used[vs[i]]) rdfs(vs[i], k++);
return k;
}
bool cross(int x, int y) {
if(a[x] < a[y] && b[x] > a[y] && b[x] < b[y]) return true;
if(a[x] < b[y] && a[x] > a[y] && b[x] > b[y]) return true;
return false;
}
void run(){
for(int i = 1; i <= m; i++) {
cin >> a[i] >> b[i];
if(a[i] > b[i]) swap(a[i], b[i]);
}
for(int i = 1; i <= m; i++) {
for(int j = i + 1; j <= m; j++) {
if(cross(i, j)) {
adde(2 * i - 1, 2 * j);
adde(2 * i, 2 * j - 1);
adde(2 * j - 1, 2 * i);
adde(2 * j, 2 * i - 1);
}
}
}
scc();
for(int i = 1; i <= m; i++) {
if(bel[2 * i] == bel[2 * i - 1]) {
cout << "the evil panda is lying again" << '\n';
return;
}
}
cout << "panda is telling the truth..." << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> m) run();
return 0;
}
【poj3207】Ikki's Story IV - Panda's Trick(2-sat)的更多相关文章
- 【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问题)
POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题) Description liympanda, one of Ikki's friend, likes ...
- 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)
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: 131072KB 64bit IO Format: %I64d ...
- 【Luogu4921】情侣?给我烧了!(组合计数)
[Luogu4921]情侣?给我烧了!(组合计数) 题面 洛谷 题解 很有意思的一道题目. 直接容斥?怎么样都要一个平方复杂度了. 既然是恰好\(k\)对,那么我们直接来做: 首先枚举\(k\)对人出 ...
- 【BZOJ4991】我也不知道题目名字是什么(线段树)
[BZOJ4991]我也不知道题目名字是什么(线段树) 题面 BZOJ 题解 对于线段树维护的区间维护以下东西: 区间左(右)端开始(结束)的最长(短)子串的长度 左端右端的值,以及当前区间内的答案 ...
- 【python】Leetcode每日一题-直方图的水量(接雨水)
[python]Leetcode每日一题-直方图的水量(接雨水) [题目描述] 给定一个直方图(也称柱状图),假设有人从上面源源不断地倒水,最后直方图能存多少水量?直方图的宽度为 1. 上面是由数组 ...
随机推荐
- [20191115]oracle实例占用内存计算.txt
[20191115]oracle实例占用内存计算.txt --//以前学习oracle数据库时,总想了解实例占用内存多少,我曾经在一些会议底下问过一位高手,对方说计算这个相对很难,许多东西是共享的.- ...
- MySQL问题记录——2003-Can't connect to MySQL server on 'localhost'(10038)
MySQL问题记录——2003-Can't connect to MySQL server on 'localhost'(10038) 摘要:本文主要记录了连接到MySQL数据库时出现的问题以及解决办 ...
- 克服悲伤情绪的三个P原则
1.自责(Personalization) --不要自责 2.永久化(Permanence) --悲伤不会永远存在,一切都会过去 据科学研究发现:人遇到开心或悲伤的事情之后,心情在短期内会产生巨大的波 ...
- 《Web Development with Go》写一个简单的LoggingMiddleware
main.go package main import ( "fmt" "log" "net/http" "time" ...
- RabbitMQ学习笔记(四、RabbitMQ队列)
目录: 消息路由失败了会怎样 备份交换器 TTL与DLX 如何实现延迟队列 RabbitMQ的RPC实现 持久化 事务 发送方确认机制 消息路由失败了会怎样: 在RabbitMQ中,如果消息路由失败了 ...
- WPF 精修篇 属性触发器
原文:WPF 精修篇 属性触发器 属性触发器是通过 某个条件触发改变属性 通过无代码实现功能 <Style TargetType="{x:Type Label}"> ...
- HTML页面导入模板页面(Tomcat)
找了个前端模板,多个HTML中有重复的部分,一改都改,所以对其进行重构,将重复的部分拿出来 看了很多方法,最简单的是jQuery,但是在我这没起作用,后来发现一个配置tomcat的ssi,让服务器帮我 ...
- golang:exported function Script should have comment or be unexported
当自己定义的包被外部使用时,如果不遵循一定的规范,那么会出现讨厌的绿色纹条,还会警告: 虽然不会影响运行,但是也令人讨厌,那么如何解决这个问题呢? 为结构体或者变量或者方法添加注释,并且开头必须是结构 ...
- 现象:SpringApplication.run后面的语句未执行
下面的两种情况下,红色的log.info中的内容一直没有执行,和预期不符. 看来,需要在@PostConstruct修饰的函数.CommandLineRunner的run方法中调用 另外的线程 来执行 ...
- python同名函数同名参数问题
如果python有两个函数的函数名与参数列表都相同那么调用该函数时,哪个函数在后,则哪个被最终调用. 举例如下: def test(): print "before hello" ...