PKU-2723 Get Luffy Out(2-SAT+二分)
Get Luffy Out
题目链接
Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by Pirate Arlong. Ratish set off at once to Arlong's island. When he got there, he found the secret place where his friend was kept, but he could not go straight in. He saw a large door in front of him and two locks in the door. Beside the large door, he found a strange rock, on which there were some odd words. The sentences were encrypted. But that was easy for Ratish, an amateur cryptographer. After decrypting all the sentences, Ratish knew the following facts:
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
There are several test cases. Every test case starts with a line containing two positive integers N (1 <= N <= 210) and M (1 <= M <= 211) separated by a space, the first integer represents the number of types of keys and the second integer represents the number of doors. The 2N keys are numbered 0, 1, 2, ..., 2N - 1. Each of the following N lines contains two different integers, which are the numbers of two keys in a pair. After that, each of the following M lines contains two integers, which are the numbers of two keys corresponding to the two locks in a door. You should note that the doors are given in the same order that Ratish will meet. A test case with N = M = 0 ends the input, and should not be processed.
Output
For each test case, output one line containing an integer, which is the maximum number of doors Ratish can open.
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
题意:
其实就是要有n对钥匙,钥匙用了一把就不能用另一把,还有m个门,每个门能用某两把钥匙开门,
问最多开多少个门
解题思路:
对于那n对钥匙中的某对钥匙\(K1,K2\),有关系\(K1\longrightarrow\overline{K2}\)以及\(K2\longrightarrow\overline{K1}\),表示用了K1就不能用K2,用了K2就不能用K1,对于每扇门也有相应的关系,如果不用其中的一把钥匙打开门\(A\),就必须用另一把钥匙\(B\)打开另一扇门,就是关系\(\overline{A}\longrightarrow B\)以及\(\overline{B}\longrightarrow
A\),根据这些关系二分能开多少门,每次重新建图并check即可
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <set>
#include <vector>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <climits>
#include <queue>
#include <stack>
using namespace std;
/* freopen("k.in", "r", stdin);
freopen("k.out", "w", stdout); */
// clock_t c1 = clock();
// std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define de(a) cout << #a << " = " << a << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int, int> VII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 4e3 + 7;
const ll MAXM = 1e6 + 7;
const ll MOD = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
int head[MAXN << 2];
struct Edge
{
int v, Next;
} e[MAXN << 2];
int dfn[MAXN << 1], low[MAXN << 1], sta[MAXN << 1], top = -1, vis[MAXN << 1];
int cnt = -1;
int num[MAXN << 1];
int tot;
int n, m;
int dep;
void add(int u, int v)
{
e[++cnt].v = v;
e[cnt].Next = head[u];
head[u] = cnt;
}
void init()
{
memset(vis, 0, sizeof(vis));
memset(dfn, 0, sizeof(dfn));
memset(head, -1, sizeof(head));
memset(low, 0, sizeof(low));
memset(num, 0, sizeof(num));
top = -1;
cnt = -1;
dep = 0;
tot = 0;
}
struct node
{
int k1, k2;
} Lock[MAXN], Key[MAXN];
void tarjan(int now)
{
dfn[now] = low[now] = ++dep;
sta[++top] = now;
vis[now] = 1;
for (int i = head[now]; ~i; i = e[i].Next)
{
int v = e[i].v;
if (!dfn[v])
{
tarjan(v);
low[now] = min(low[now], low[v]);
}
else if (vis[v])
low[now] = min(low[now], dfn[v]);
}
if (dfn[now] == low[now])
{
tot++;
while (sta[top] != now)
{
num[sta[top]] = tot;
vis[sta[top--]] = 0;
}
vis[sta[top]] = 0;
num[sta[top--]] = tot;
}
} //强连通分量的标号来得到反向的拓扑序
bool check(int x)
{
init();
for (int i = 1; i <= n; i++)
{
add(Key[i].k1, Key[i].k2 + 2 * n);
add(Key[i].k2, Key[i].k1 + 2 * n);
}
for (int i = 1; i <= x; i++)
{
add(Lock[i].k1 + 2 * n, Lock[i].k2);
add(Lock[i].k2 + 2 * n, Lock[i].k1);
}
for (int i = 0; i < (n << 1); i++)
if (!dfn[i])
tarjan(i);
for (int i = 0; i < (n << 1); i++)
if (num[i + 2 * n] == num[i])
return false;
return true;
}
int main()
{
while (~scanf("%d%d", &n, &m) && n + m)
{
for (int i = 1; i <= n; i++)
scanf("%d%d", &Key[i].k1, &Key[i].k2);
for (int i = 1; i <= m; i++)
scanf("%d%d", &Lock[i].k1, &Lock[i].k2);
int l = 0, r = m;
while (l < r)
{
int mid = (l + r + 1) / 2;
if (check(mid))
l = mid;
else
r = mid - 1;
}
printf("%d\n", l);
}
return 0;
}
PKU-2723 Get Luffy Out(2-SAT+二分)的更多相关文章
- POJ 2723 Get Luffy Out(2-SAT+二分答案)
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8851 Accepted: 3441 Des ...
- 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
题目链接 给n个钥匙对, 每个钥匙对里有两个钥匙, 并且只能选择一个. 有m扇门, 每个门上有两个锁, 只要打开其中一个就可以通往下一扇门. 问你最多可以打开多少个门. 对于每个钥匙对, 如果选择了其 ...
- 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问题
Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was ca ...
- TTTTTTTTTTTTTTTT POJ 2723 楼层里救朋友 2-SAT+二分
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3162 Des ...
- poj 2723 Get Luffy Out 2-SAT
两个钥匙a,b是一对,隐含矛盾a->!b.b->!a 一个门上的两个钥匙a,b,隐含矛盾!a->b,!b->a(看数据不大,我是直接枚举水的,要打开当前门,没选a的话就一定要选 ...
- [转] POJ图论入门
最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...
- 图论常用算法之一 POJ图论题集【转载】
POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
随机推荐
- Ubuntu安装微信,解决deepin“版本过低”或NO_PUBKEY问题
在搜索引擎搜索Ubuntu安装微信,最多的结果是通过deepin安装 但是里面使用的deepin-for-ubuntu 安装之后微信扫码会提示版本过低 直接安装deepin.com.wechat_2. ...
- Python_全局变量的定义
1.在my套件下新建一个关键字systemkey并进行脚本的编写:创建一个${var1}变量,并赋值为aaaaaaaaaa Set Global Variable ${var1} ...
- 第二阶段:2.商业需求分析及BRD:6.商业需求文档2
BRD的三个诉求:1.项目很重要,支持.2.有价值,获得重视,纳入战略规划中.3.需要资源,横向的协调资源. 方法:知道决策层是哪些组成,同时找到合适的决策层. BRD决策分类:1.找资本类(CF ...
- DOCKER学习_006:Docker存储驱动
一 镜像的分层特性 在说docker的文件系统之前,我们需要先想清楚一个问题.我们知道docker的启动是依赖于image,docker在启动之前,需要先拉取image,然后启动.多个容器可以使用同一 ...
- form表单提交方式实现浏览器导出Excel
刚开始使用ajax做Excel导出,发现ajax做不了浏览器导出只能下载到本地,于是用form提交可以提供浏览器下载Excel. 1>用ajax做本地下载: FileOutputStream f ...
- head插件安装-elasticsearch
1.安装node环境: 下载地址:https://nodejs.org/download/release/v8.13.0/node-v8.13.0-linux-x64.tar.gz gunzip n ...
- 【题解】LOJ2759. 「JOI 2014 Final」飞天鼠(最短路)
[题解]LOJ2759. 「JOI 2014 Final」飞天鼠(最短路) 考虑最终答案的构成,一定是由很多飞行+一些上升+一些下降构成. 由于在任何一个点上升或者下降代价是一样的,所以: 对于上升操 ...
- 小小知识点(三十九) 正交频分复用OFDM的基本原理及实现
引言 符号间干扰(ISI)是无线传输系统设计中需要考虑的因素,采用什么样的处理方法取决于数据传输速率或等效传输带宽来决定 (1)若数据速率低且与信道最大延迟相比符号持续时间较长,那么就可能无需任何均衡 ...
- AtomicXXX系列类使用分析
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 在java.util.concurrent.atomic ...
- iOS开发常见问题
1. 在 ViewController 中添加子视图时,导航栏遮挡添加的子视图 let bpView = BpView.init(frame: CGRect.init(x: , y: , width: ...