poj 2723 Get Luffy Out-2-sat问题
Description
Behind the large door, there is a nesting prison, which consists of M floors. Each floor except the deepest one has a door leading to the next floor, and there are two locks in each of these doors. Ratish can pass through a door if he opens either of the two locks in it. There are 2N different types of locks in all. The same type of locks may appear in different doors, and a door may have two locks of the same type. There is only one key that can unlock one type of lock, so there are 2N keys for all the 2N types of locks. These 2N keys were divided into N pairs, and once one key in a pair is used, the other key will disappear and never show up again.
Later, Ratish found N pairs of keys under the rock and a piece of paper recording exactly what kinds of locks are in the M doors. But Ratish doesn't know which floor Luffy is held, so he has to open as many doors as possible. Can you help him to choose N keys to open the maximum number of doors?
Input
Output
Sample Input
3 6
0 3
1 2
4 5
0 1
0 2
4 1
4 2
3 5
2 2
0 0
Sample Output
4 今天学习了 2-sat问题 这是一个现实中经常遇到的问题可以转化为图论去解决
有m扇门,每扇门都有两把锁,只要打开其中一把就可以打开这扇门,
打开了第i扇门才能继续开第i+1扇门。
现在有2n个钥匙,每两个绑在一起,使用了一个就不能使用另一个。
问最多能开几扇门。
这个就是 2-sat问题的模板题
以下是这类问题的基本的解题思路
- 构造有向图,这个有向图是一个表示了选择了一个点,一定要选择它能够到达的点的图。
- 对这个图求强连通图。
- 假如有两个点处在同一个强连通图内,这两个点是必须只能选择一个的一组内的点,就不可以。
- 否则可以
a->b+2*n 表示 选a不选b
门的建边就要 a+2*n->b
然后判断 a和a+2*n 在不在同一个强连通分量内
回到题目来 这题的话网上普遍二分解决 因为这样快啊
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack> using namespace std;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
const int INF = 0x7ffffff;
struct node {
int v, next;
} edge[maxn];
int head[maxn], dfn[maxn], low[maxn];
int s[maxn], belong[maxn], instack[maxn];
int tot, cnt, top, flag, n, m;
void init() {
tot = cnt = top = flag = ;
memset(s, , sizeof(s));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(instack, , sizeof(instack));
}
void add(int u, int v ) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjin(int v) {
dfn[v] = low[v] = ++flag;
instack[v] = ;
s[top++] = v;
for (int i = head[v] ; ~i ; i = edge[i].next ) {
int j = edge[i].v;
if (!dfn[j]) {
tarjin(j);
low[v] = min(low[v], low[j]);
} else if (instack[j]) low[v] = min(low[v], dfn[j]);
}
if (dfn[v] == low[v]) {
cnt++;
int t;
do {
t = s[--top];
instack[t] = ;
belong[t] = cnt;
} while(t != v) ;
}
}
struct p {
int u, v;
} qu[maxn];
int check(int mid) {
init();
for (int i = ; i < n ; i++) {
add(qu[i].v, qu[i].u + * n);
add(qu[i].u, qu[i].v + * n);
}
for (int i = ; i < mid ; i++ ) {
add(qu[i + n].u + * n, qu[i+n].v);
add(qu[i + n].v + * n, qu[i+n].u);
}
for (int i = ; i < * n ; i++)
if (!dfn[i]) tarjin(i);
for (int i = ; i < * n ; i++)
if (belong[i] == belong[i + * n]) return ;
return ;
}
int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if (n == && m == ) break;
for (int i = ; i < n ; i++)
scanf("%d%d", &qu[i].u, &qu[i].v);
for (int i = ; i < m ; i++)
scanf("%d%d", &qu[i + n].u, &qu[i + n].v);
int low = , high = m, mid;
while(low <= high) {
mid = (low + high) / ;
if (check(mid)) low = mid + ;
else high = mid - ;
}
printf("%d\n", high);
}
return ;
}
poj 2723 Get Luffy Out-2-sat问题的更多相关文章
- HDU 1816, POJ 2723 Get Luffy Out(2-sat)
HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...
- POJ 2723 Get Luffy Out(2-SAT+二分答案)
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8851 Accepted: 3441 Des ...
- poj 2723 Get Luffy Out(2-sat)
Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was ca ...
- poj 2723 Get Luffy Out 二分+2-sat
题目链接 给n个钥匙对, 每个钥匙对里有两个钥匙, 并且只能选择一个. 有m扇门, 每个门上有两个锁, 只要打开其中一个就可以通往下一扇门. 问你最多可以打开多少个门. 对于每个钥匙对, 如果选择了其 ...
- poj 2723 Get Luffy Out 2-SAT
两个钥匙a,b是一对,隐含矛盾a->!b.b->!a 一个门上的两个钥匙a,b,隐含矛盾!a->b,!b->a(看数据不大,我是直接枚举水的,要打开当前门,没选a的话就一定要选 ...
- Get Luffy Out (poj 2723 二分+2-SAT)
Language: Default Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7969 ...
- POJ 2723 HDU 1816 Get Luffy Out
二分答案 + 2-SAT验证 #include<cstdio> #include<cstring> #include<cmath> #include<stac ...
- poj 2723
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7295 Accepted: 2778 Des ...
- 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 ...
随机推荐
- python--随笔一
1.format函数--根据关键字和位置选择性插入数据 In [11]: '{mingzi}jintian{dongzuo}'.format(mingzi='duzi',dongzuo='i love ...
- 一个简单的WPF MVVM实例【转载】
引用地址:http://blog.csdn.net/yl2isoft/article/details/20838149 1 新建WPF 应用程序WPFMVVMExample 程序结构如下图所示. 2 ...
- flask-login原理详解
最近发现项目中使用的flask-login中有些bug,直接使用官网的方式确实可以用,但仅仅是可以用,对于原理或解决问题没有什么帮助,最近通过查看网上资料.分析源码.通过demo.从零开始总结了fla ...
- linux c 调用子文件函数
今天在学习初级linux c的时候遇到了如下问题:通过主函数调用同路径下的子文件函数调用失败.博主是这样一一解决的: 首先:hello.c: hello.c: #include<bool.c&g ...
- 【转帖】置高并发jdbc连接池
简单的MySQL连接池 <Resource type="javax.sql.DataSource" name="jdbc/TestDB" factory= ...
- 非阻塞IO模板
服务端 from socke import * server = socket(AF_INET, SOCK_STREAM) server.bind(('127.0.0.1',8083)) server ...
- Java - ArrayList List 等迭代集合执行移除(remove) 的正确方法
方法1: List<String> al = new ArrayList<String>(); Iterator<String> it = al.iterator( ...
- 剑指Offer - 九度1516 - 调整数组顺序使奇数位于偶数前面
剑指Offer - 九度1516 - 调整数组顺序使奇数位于偶数前面2013-11-30 02:17 题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部 ...
- 架构师速成6.7-设计开发思路-uml 分类: 架构师速成 2015-07-29 18:25 157人阅读 评论(0) 收藏
uml是什么东西?统一建模语言,一门语言,是用来进行软件设计的一门语言. 其实一门语言的诞生并不伟大,让大多数人都使用才足够伟大.uml就是一门伟大的语言,因为目前软件设计的唯一语言就是它. UML其 ...
- Windows Server 2008 R2 集群(OpenService “RemoteRegistry” 失败)笔记
OpenService “RemoteRegistry” 失败. 我在创建验证域控服务器[系统]类别中 看到错误日志 我在域控服务器去看,在 computers 里面 是有这台 计算机,但是为什么不行 ...