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 ...
随机推荐
- Linux实战教学笔记05:远程SSH连接服务与基本排错
第1章 远程连接LInux系统管理 1.1 为什么要远程连接Linux系统 在实际的工作场景中,虚拟机界面或物理服务器本地的窗口都是很少能够接触到的,因为服务器装完系统后,都要拉到IDC机房托管,如果 ...
- 微信小程序相关
https://www.cnblogs.com/shenzikun1314/p/7805168.html
- Aizu:0009- Prime Number
Prime Number Time limit 1000 ms Memory limit 131072 kB Problem Description Write a program which rea ...
- CentOS 使用 LAMP 环境开启 SSL 搭建 WordPress
环境阿里云新装CentOS 7.4, 使用yum(非编译安装)搭建LAMP, CA证书为阿里云免费提供的, WordPress为官网下载 安装 LAMP 并开启 HTTPS 1, 关闭防火墙 # sy ...
- 十一、mysql老是停止运行该怎么解决
mysql老是停止运行该怎么解决 你可能还会遇到无法启动mysql的错误 解决方法如下:
- netty学习记录1
最近在学习netty,看的是<netty权威指南 第2版>. 然后看的同时也把书上面的代码一行行敲下来做练习,不过到第三章就出问题了. 按照书上讲的,sever/client端都需要继承C ...
- CC3200模块的内存地址划分和bootloader,启动流程(二)
1. 首先启动内部ROM固化的BOOT,然后这个ROM启动需要使用内存空间0X2000 0000 --- 0X2000 4000共16K的空间.一级BOOT的作用是串口升级和驱动库. 2. 然后是二级 ...
- 使用Matrix-tree与它的行列式来解决生成树计数问题
我又把Matrix写错啦 这东西讲课的时候竟然一笔带过了,淦 好吧这东西我不会证 那我们来愉快的看结论吧 啦啦啦 预备工作 你有一个 $ n $ 个点的图 比如说 5 /|\ / | \ 2--1-- ...
- 《Cracking the Coding Interview》——第17章:普通题——题目2
2014-04-28 22:05 题目:写个程序判断三连棋哪一方赢了. 解法:三个相同的棋子连成一条横线,竖线或者对角线就判断为赢了. 代码: // 17.2 Write an algorithm t ...
- java中封装的概念
封装(Encapsulation)是类的三大特性之一, 就是将类的状态信息隐藏在类的内部,不允许外部程序直接访问, 而是通过该类提供的方法来实现对隐藏信息的操作和访问. 简而言之,就是隐藏内部实现,提 ...