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 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?
输入格式
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.
输出格式
For each test case, output one line containing an integer, which is the maximum number of doors Ratish can open.
输入样例
3 6
0 3
1 2
4 5
0 1
0 2
4 1
4 2
3 5
2 2
0 0
输出样例
4
题解
看题看了好久系列。。。。
首先我们先确定是以哪种东西为点
钥匙二选一,显然钥匙是点
门是对应的限制关系:每个门对应的两种钥匙至少选一种,就是OR关系
OR关系:若\(a OR b = true\),则a'->b,b'->a【因为如果a是假,由于a OR b为真,b一定为真,另一个同理】
题目要求我们求最多开的门数
我们知道2-sat最常用的作用就是判定,所以2-sat可以作为二分的判定,进行二分答案
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
#define cls(x) memset(x,0,sizeof(x))
using namespace std;
const int maxn = 3005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
return out * flag;
}
int h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
void build(int u,int v){ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;}
int dfn[maxn],low[maxn],Scc[maxn],st[maxn],scci,top,cnt;
void dfs(int u){
dfn[u] = low[u] = ++cnt;
st[++top] = u;
Redge(u){
if (!dfn[to = ed[k].to]){
dfs(to);
low[u] = min(low[u],low[to]);
}else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
}
if (dfn[u] == low[u]){
scci++;
do{Scc[st[top]] = scci;}while (st[top--] != u);
}
}
int n,m,op[maxn],A[maxn],B[maxn];
void init(){
cls(dfn); cls(Scc); cls(h); ne = 1; scci = cnt = top = 0;
}
bool check(int len){
init();
REP(i,len){
if (op[A[i]] == B[i]) continue;
else if (A[i] == B[i]) build(op[A[i]],A[i]);
else build(op[A[i]],B[i]),build(op[B[i]],A[i]);
}
for (int i = 0; i < (n << 1); i++) if (!dfn[i]) dfs(i);
for (int i = 0; i < n; i++) if (Scc[i] == Scc[op[i]]) return false;
return true;
}
int main(){
while (~scanf("%d%d",&n,&m) && n){
int a,b;
REP(i,n) a = read(),b = read(),op[a] = b,op[b] = a;
REP(i,m) A[i] = read(),B[i] = read();
int L = 1,R = m,mid;
while (L < R){
mid = L + R + 1 >> 1;
if (check(mid)) L = mid;
else R = mid - 1;
}
printf("%d\n",L);
}
return 0;
}
POJ2723 Get Luffy Out 【2-sat】的更多相关文章
- Python开发【第一章】:Python简介和入门
Python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种继承. ...
- 【WebApi系列】浅谈HTTP
[01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi如何传递参数 [04]详解WebApi测试和PostMan [05]浅谈WebApi Core ...
- 【WebApi系列】浅谈HTTP在WebApi开发中的运用
WebApi系列文章 [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi参数的传递 [04]详解WebApi测试和PostMan [05]浅谈W ...
- 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型
前文提要:[js实例]js中的5种基本数据类型和9种操作符 Array类型的9个数组方法 Array中有9个数组方法: 1.检测数组 2.转换方法 3.栈方法 4.队列方法 5.冲排序方法6.操作方法 ...
- Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】
Spring Cloud(三):服务提供与调用 Eureka[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 上一篇文章我们介绍了 Eureka 服务 ...
- 【插头DP】BZOJ1187- [HNOI2007]神奇游乐园
[题目大意] 在n*m的网格中选一条回路,使权值和最大. [思路] 和之前裸的插头DP差不多,只不过现在回路不需要经过所有的格子.所以有以下几个注意点(具体看注释): (1)left和up插头相等的时 ...
- zz【清华NLP】图神经网络GNN论文分门别类,16大应用200+篇论文最新推荐
[清华NLP]图神经网络GNN论文分门别类,16大应用200+篇论文最新推荐 图神经网络研究成为当前深度学习领域的热点.最近,清华大学NLP课题组Jie Zhou, Ganqu Cui, Zhengy ...
- 【故障解决】OGG-00446 错误解决
[故障解决]OGG-00446 Could not find archived log for sequence 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读和 ...
- 【故障解决】ORA-06502错误解决
[故障解决]ORA-06502: PL/SQL: numeric or value error: character string buffer too small 一.1 BLOG文档结构图 ...
随机推荐
- HTML 5.1 的 14 个新特性(含使用案例)
HTML5 属于万维网联盟 (W3C), 这个组织为整个网络界提供了标准,如此形成的协议可在全世界通行.在 2016 年 11 月, W3C 对长期行使的 HTML 5 标准进行了更新,它是2年内的第 ...
- Nodejs:Node.js模块机制小结
今天读了<深入浅出Nodejs>的第二章:模块机制.现在做一个简单的小结. 序:模块机制大致从这几个部分来讲:JS模块机制的由来.CommonJS AMD CMD.Node模块机制和包和n ...
- vue框架初学习的基本指令
学习地址:<ahref="https: cn.vuejs.="" org="" "="" targe ...
- Sass和gulp的简单了解
一.sass less css预处理器 sass里面有2种语法 第一种语法是sass 后缀名必须是sass 第二种语法是scss 后缀名必须是scss ...
- Java 对数组的筛选
在Java里面 一般对一个数组进行筛选,去剔除一些元素,一般做法是用临时数组来存储,把符合条件的元素加入到新数组中,虽然数组有移除的方法但是 是线程不安全的: 而用迭代器Iterator,可以在遍历的 ...
- yum安装报错
检查了好久才知道原来是 sudo nano /etc/sysconfig/network-scripts/ifcfg-ens33 下的DNS配错了,改好之后,sudo service network ...
- pandas知识点(处理缺失数据)
pandas使用浮点值NaN表示浮点和非浮点数组中的缺失数据: In [14]: string_data = Series(['aardvark','artichoke',np.nan,'avocad ...
- HTML5——7个最牛的HTML5移动开发框架
月的iPhoneDevCamp上写成的.创建它的一个主要动力是基于一个几乎每一个单独的iPhone开发新手都要面对的简单事实:Objective-C是一个对Web开发人员来说非常陌生的环境,并且Web ...
- OpenCV学习笔记(五) 文件存取
转自输入输出XML和YAML文件 To be filled
- Java-数据结构之栈练习
栈(stack)可以看做是特殊类型的线性表,访问.插入和删除其中的元素只能在栈尾(栈顶)进行. 队列(queue)表示一个等待的线性表,它也可以看做是一种特殊类型的线性表,元素只能从队列的末端(队列尾 ...