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 =============================以下是最小生成树+并 ...
随机推荐
- sql查询分类和所有子类
select * from [JianDu].[dbo].[ZuZhiJiGou] where id = 64 --查询节点 union allSELECT TOP 1000 a.* FROM [Ji ...
- PHP常用函数拾遗
PHP中常用过滤函数addslashes().mysql_real_escape_string().mysql_escape_string() 如addslashes().mysql_real_es ...
- 在Spring Boot中使用Docker在测试中进行高级功能测试
最近又学到了很多新知识,感谢优锐课老师细致地讲解,这篇博客记录下自己所学所想. 想更多地了解Spring Boot项目中的功能测试吗?这篇文章带你了解有关在测试中使用Docker容器的更多信息. 本文 ...
- MongoDB not authorized for query - code 13 错误解决办法
跟着教程走完到了鉴权阶段,不加 --auth 登陆正常,但会出现warning :没有鉴权,修改不会生效,此时登陆正常. 但是加上了--auth 启动之后加上密码登陆则无法登陆. 添加用户和鉴权: 先 ...
- JMM&Thread
1.概述 高效并发通过JAVA线程之间提高并发协调实现,在实现过程中需考虑硬件的效率和一致性,但在运算的过程中需要考虑处理器与内存的交互,所以基于高速缓存的存储交互解决的处理器与内存的方案,在对多处理 ...
- vc++ 监控指定路径下文件变化
参考MSDN文档 https://docs.microsoft.com/zh-cn/windows/desktop/api/winbase/nf-winbase-readdirectorychange ...
- 前端模块化——彻底搞懂AMD、CMD、ESM和CommonJS
我们知道,在NodeJS之前,由于没有过于复杂的开发场景,前端是不存在模块化的,后端才有模块化.NodeJS诞生之后,它使用CommonJS的模块化规范.从此,js模块化开始快速发展. 模块化的开发方 ...
- [gitHub实践] git基础:远程仓库的使用
[gitHub实践] git基础:远程仓库的使用 版权2019.6.2更新 git 基础 远程仓库的使用 git remote # 查看远程仓库 $ git remote # 克隆的仓库服务器默认名字 ...
- 记: Spring Data Jpa @OneToMany 级联查询被动触发的问题
I have encountered a bug in using Spring Data Jpa. Specifically,when @OneToMany was used to maintain ...
- Jenkins2构建pipeline流水线
流水线有两种方式: 1.脚本式流水线 2.声明式流水线 构建流水线的简单示例: 脚本式流水线 node ('master'){ stage("Source"){ //从Git仓库中 ...