题面描述

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?

题意

给你 n 对钥匙,每对若用了其中一把,另一把就不能用。有 m 扇门,每扇门可以由给定的两把钥匙中的任意一把打开,问最多能打开多少扇门。

思路

其实可以不用二分,但二分跑的快些。

二分答案,对于每对钥匙 \(a\) 和 \(b\),\(a\) 用了 \(b\) 就不能用(\(a \rightarrow \neg b\)),\(b\) 用了 \(a\) 就不能用(\(b \rightarrow \neg a\))。

对于每扇门的 \(a\) 和 \(b\),不用 \(a\) 打开就必须用 \(b\) 打开(\(\neg a \rightarrow b\)),不用 \(b\) 打开就必须用 \(a\) 打开(\(\neg b \rightarrow a\))。

所以建个图,跑个 2-SAT 就好啦

代码

/************************************************
*Author : lrj124
*Created Time : 2019.11.10.20:21
*Mail : 1584634848@qq.com
*Problem : poj2723
************************************************/
#include <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
const int maxn = 10000 + 10;
int n,m,low[maxn],dfn[maxn],scc[maxn],scccnt,ind;
pair<int,int> key[maxn],door[maxn];
vector<int> edge[maxn];
bool vis[maxn];
stack<int> s;
inline void tarjan(int now) {
dfn[now] = low[now] = ++ind;
vis[now] = true,s.push(now);
for (size_t i = 0;i < edge[now].size();i++) {
int to = edge[now][i];
if (!dfn[to]) {
tarjan(to);
low[now] = min(low[now],low[to]);
} else if (vis[to]) low[now] = min(low[now],dfn[to]);
}
if (dfn[now] == low[now]) {
scc[now] = ++scccnt;
for (;s.top() ^ now;vis[s.top()] = false,s.pop()) scc[s.top()] = scccnt;
vis[now] = false,s.pop();
}
}
inline bool check(int mid) {
ind = scccnt = 0;
for (int i = 1;i <= 4*n;i++) edge[i].clear(),low[i] = dfn[i] = 0;
for (int i = 1;i <= n;i++) {
edge[key[i].first].push_back(key[i].second+2*n);
edge[key[i].second].push_back(key[i].first+2*n);
}
for (int i = 1;i <= mid;i++) {
edge[door[i].first+2*n].push_back(door[i].second);
edge[door[i].second+2*n].push_back(door[i].first);
}
for (int i = 1;i <= 4*n;i++) if (!dfn[i]) tarjan(i);
for (int i = 1;i <= 2*n;i++) if (scc[i] == scc[i+2*n]) return false;
return true;
}
int main() {
// freopen("poj2723.in","r",stdin);
// freopen("poj2723.out","w",stdout);
for (;scanf("%d%d",&n,&m),n && m;) {
for (int i = 1,x,y;i <= n;i++) {
scanf("%d%d",&x,&y); x++,y++;
key[i] = make_pair(x,y);
}
for (int i = 1,x,y;i <= m;i++) {
scanf("%d%d",&x,&y); x++,y++;
door[i] = make_pair(x,y);
}
int l = 0,r = m,ans;
for (int mid;l <= r;check(mid = l+r>>1) ? l = mid+1,ans = mid : r = mid-1);
printf("%d\n",ans);
}
return 0;
}

【POJ2723】Get Luffy Out - 二分+2-SAT的更多相关文章

  1. hdu3715 Go Deeper[二分+2-SAT]/poj2723 Get Luffy Out[二分+2-SAT]

    这题转化一下题意就是给一堆形如$a_i + a_j \ne c\quad (a_i\in [0,1],c\in [0,2])$的限制,问从开头开始最多到哪条限制全是有解的. 那么,首先有可二分性,所以 ...

  2. POJ2723 Get Luffy Out解题报告tarjan+2-SAT+二分

    今天看到讲2-SAT比较好的blog,感觉微微的理解了2-SAT 传送门 参考: https://blog.csdn.net/leolin_/article/details/6680144 题意:你有 ...

  3. POJ2723 Get Luffy Out 【2-sat】

    题目 Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by P ...

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

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

  5. HDU - 1816 Get Luffy Out *(二分 + 2-SAT)

    题目大意:有N串钥匙,M对锁.每串钥匙仅仅能选择当中一把.怎样选择,才干使开的锁达到最大(锁仅仅能按顺序一对一对开.仅仅要开了当中一个锁就可以) 解题思路:这题跟HDU - 3715 Go Deepe ...

  6. poj2723 2sat判断解+二分

    典型的2-sat问题,题意:有m个门,每个门上俩把锁,开启其中一把即可,现在给n对钥匙(所有 钥匙编号0123456...2n-1),每对钥匙只能用一把,要求尽可能开门多(按顺序,前max个). 关键 ...

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

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

  8. Get Luffy Out (poj 2723 二分+2-SAT)

    Language: Default Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7969   ...

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

随机推荐

  1. mysqldump导出表结构及数据

    问题描述:有需要mysql某几张表的需求,某个数据库某几张表,导出先检查相应的数据库和表是否存在 数据泵用法:默认导出的是表结构以及表中的数据 mysqldump -uroot -p -S /data ...

  2. Makefile中的奇葩字符

    % : Makefile规则通配符,一般出现在目标或是依赖中 * : shell命令中的通配符,一般出现在命令中 $@:目标的名字 $^:所有依赖的名字 $<:第一个依赖的名字 $?:所有依赖中 ...

  3. 动手实现一个简单的 rpc 框架到入门 grpc (下)

    之前手动实现了一次简陋的 rpc 调用,为了简单使用了 json 编码信息,其实这是非常不可靠的,go 中 json 解析会有一些问题,比如整数会变成浮点数,而且 json 字符串比较占空间. gRP ...

  4. 如何获取json某一级节点的数据

    如何获取json某一级节点的数据 最近做项目有获取和设置固定格式某一级节点值的需求.但是要一级一级地取对于多级的结构来说代码过于冗余且重复,于是写了个递归的方法根据json路径完成值的定点操作.废话不 ...

  5. 玩LOL间歇性卡顿(FPS突然降低又马上恢复)?Windows10间歇性卡顿?

    一..问题描述: LOL时:画面突然死掉,不能操作:FPS突然降低,从三位数降到两位数(150 -> 6).我最开始就是从LOL这里观测到的,因为游戏是卡顿最直观.最明显的表现.之后才发现不玩游 ...

  6. MySQL数据管理

    3.MySQL数据管理 3.1外键 方式一:  create table `grade`(  `gradeid` int(10) not null auto_increment comment '年纪 ...

  7. 前端学习(十五):了解 Javascript

    进击のpython ***** 前端学习--了解JavaScript Javascript是一种运行在浏览器中的解释型的编程语言 还记得我们在说python的时候提过解释型和编译型的区别 在解释型语言 ...

  8. Day01_搭建环境&CMS服务端开发

    学成在线 第1天 讲义-项目概述 CMS接口开发 1 项目的功能构架 1.1 项目背景 受互联网+概念的催化,当今中国在线教育市场的发展可谓是百花齐放.如火如荼. 按照市场领域细分为:学前教育.K12 ...

  9. Qt高级编程 高清PDF+源|网盘下载地址附提取码|

    书籍作者:Mark Summerfield(马克 . 萨默菲尔德)(英)   书籍译者:闫锋欣内容简介:本书是一本阐述Qt高级编程技术的书籍.本书以工程实践为主旨,是对Qt现有的700多个类和上百万字 ...

  10. luogu P5289 [十二省联考2019]皮配 背包

    LINK:皮配 我承认是一道很难的题目. 不过对于这道题 部分分的提示显得尤为重要. 首先是 40分的暴力dp 很容易想 但是不容易写. 从40分可以发现我们只需要把蓝阵营和鸭派系的人数给存在起来就行 ...