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+二分)的更多相关文章

  1. POJ 2723 Get Luffy Out(2-SAT+二分答案)

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8851   Accepted: 3441 Des ...

  2. HDU 1816, POJ 2723 Get Luffy Out(2-sat)

    HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...

  3. poj 2723 Get Luffy Out 二分+2-sat

    题目链接 给n个钥匙对, 每个钥匙对里有两个钥匙, 并且只能选择一个. 有m扇门, 每个门上有两个锁, 只要打开其中一个就可以通往下一扇门. 问你最多可以打开多少个门. 对于每个钥匙对, 如果选择了其 ...

  4. 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 ...

  5. 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 ...

  6. TTTTTTTTTTTTTTTT POJ 2723 楼层里救朋友 2-SAT+二分

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8211   Accepted: 3162 Des ...

  7. poj 2723 Get Luffy Out 2-SAT

    两个钥匙a,b是一对,隐含矛盾a->!b.b->!a 一个门上的两个钥匙a,b,隐含矛盾!a->b,!b->a(看数据不大,我是直接枚举水的,要打开当前门,没选a的话就一定要选 ...

  8. [转] POJ图论入门

    最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...

  9. 图论常用算法之一 POJ图论题集【转载】

    POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...

  10. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

随机推荐

  1. lombok工作原理分析

    在Lombok使用的过程中,只需要添加相应的注解,无需再为此写任何代码.但是自动生成的代码到底是如何产生的呢? 核心之处就是对于注解的解析上.JDK5引入了注解的同时,也提供了两种解析方式. 运行时解 ...

  2. 如何查看linux是否打开虚拟化

    [服务器虚拟化] 执行命令: cat /proc/cpuinfo |grep vmx     如果有输出,则说明CPU支持虚拟化技术. --------> svm - Secure virtua ...

  3. 【python测试开发栈】帮你总结python random模块高频使用方法

    随机数据在平时写python脚本时会经常被用到,比如随机生成0和1来控制逻辑.或者从列表中随机选择一个元素(其实抽奖程序也类似,就是从公司所有人中随机选择中奖用户)等等.这篇文章,就帮大家整理在pyt ...

  4. 洛谷$P2486\ [SDOI2011]$染色 线段树+树链剖分

    正解:线段树+树链剖分 解题报告: 传送门$QwQ$ 其实是道蛮板子的题,,,但因为我写得很呆然后写了贼久之后发现想法有问题要重构,就很难受,就先写个题解算了$kk$ 考虑先跑个树剖,然后按$dfn$ ...

  5. 1051 复数乘法 (15 分)C语言

    复数可以写成 (A+Bi) 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 i^​2=−1:也可以写成极坐标下的指数形式 (R×e​(Pi)​ ),其中 R 是复数模,P 是辐角,i ...

  6. SpringBoot项目的代理机制【一】

    这是了解Spring代理机制的第一篇,尝试了解Spring如何实现Bean的注册和代理.这篇文章会抛出问题:Spring注册Bean,都会用Jdk代理或cglib创建代理对象吗? 1 项目准备 1.1 ...

  7. Ubuntu 18.04 + ROS Melodic + TurtleBot3仿真

    1. 下载安装包 官网地址: http://wiki.ros.org/action/show/Robots/TurtleBot?action=show&redirect=TurtleBot 所 ...

  8. (一)Django项目架构介绍

    项目的架构为: 1.虚拟环境virtualenv 安装Django==2.1.3 安装pymysql 安装mysqlclient 安装其他等 2.项目结构为: 应用APP: blog -- 管理博客 ...

  9. 二、Spring Cloud之注册中心 Eureka

    前言 算是正式开始学习 spring cloud 的项目知识了,大概的知道Springcloud 是由众多的微服务组成的,所以我们现在一个一个的来学习吧. 注册中心,在微服务中算是核心了.所有的服务都 ...

  10. creator 2.0版本对于preloadScene函数获取加载进度

    有时候,当我们场景上挂载的资源过多时,我们使用cc.director.loadScene切换场景时会等一段时间才会切换过去,这对游戏的体验是相当不好的.所以我们可以使用cc.director.prel ...