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 =============================以下是最小生成树+并 ...
随机推荐
- C语言中的优先级和类型转换分析
一.优先级 1.易错的优先级 二.类型转换 在C语言中,存在强制类型装换,也存在隐式类型转换,隐式类型转换实际上属于强制类型转换,隐式类型转换要点如图. (1)举例:算术运算式中,低类型转换为高类型 ...
- Mybatis 多对多(易百教程)
mybatis3.0 添加了association和collection标签专门用于对多个相关实体类数据进行级联查询,但仍不支持多个相关实体类数据的级联保存和级联删除操作.因此在进行实体类多对多映射表 ...
- Javascript事件系统
本文内容 事件基础 事件监听方式 事件默认行为 事件冒泡与事件捕获 事件绑定与事件委托 事件基础 注意:本文不会深入探究Javascript的事件循环. 提到事件,相信每位Javascript开发者都 ...
- 【题解】LOJ2759. 「JOI 2014 Final」飞天鼠(最短路)
[题解]LOJ2759. 「JOI 2014 Final」飞天鼠(最短路) 考虑最终答案的构成,一定是由很多飞行+一些上升+一些下降构成. 由于在任何一个点上升或者下降代价是一样的,所以: 对于上升操 ...
- [03]java中的方法以及控制语句
00 Java中的语句块 语句块(有时叫做复合语句),是用花括号扩起的任意数量的简单Java语句.块确定了局部变量的作用域.块中的程序代码,作为一个整体,是要被一起执行的.块可以被嵌套在另一个块中,但 ...
- 如何让接口文档自动生成,SpringBoot中Swagger的使用
目录 一.在SpringBoot项目中配置Swagger2 1.pom.xml中对Swagger2的依赖 2.编写配置类启用Swagger 3.配置实体类的文档 4.配置接口的文档 5.访问文档 二. ...
- 「Luogu P2015」二叉苹果树 解题报告
题面 一个二叉树,边数为n\((2<n\le 100)\),每条边有一个权值,求剪枝后剩下p\((1<p<n)\)条边,使p条边的权值和最大 还看不懂?-- 2 5 input:5 ...
- 使用Theia——添加语言支持
上一篇:使用Theia——创建插件 Theia——添加语言支持 Theia中TextMate的支持 使用TextMate语法可以为大部分源文件提供精准的着色修饰,虽然这只是在语法级别上(没有语言本身的 ...
- Linux学习_菜鸟教程_3
我是在UBANTO上运行Linux的,开机启动时按下shift或者Esc都不能进入到grub,没有百度到可靠的教程. 暂时先这样吧.免得我把系统搞坏了,先学点实用的知识~~ Next Chapter
- vnpy源码阅读学习(1):准备工作
vnpy源码阅读学习 目标 通过阅读vnpy,学习量化交易系统的一些设计思路和理念. 通过阅读vnpy学习python项目开发的一些技巧和范式 通过vnpy的设计,可以用python复现一个小型简单的 ...