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文档结构图 ...
随机推荐
- 在ListView控件中实现修改功能
实现效果: 知识运用: ListView控件的LabelEdit属性 //指示用户是否可以编辑控件中数据项的标签 public bool LabelEdit{get;set;} 实现代码: priva ...
- SunmmerVocation_Learning--Java数组的创建
一维数组声明方式: type var[] 或 type[] var; 如int a[], int[] a; Java中声明数组不能指定其长度,如int a[5]是非法的. 一维数组对象的创建: Jav ...
- mysql索引详细描述与应用场景
索引的数据结构: (1)一般是B+tree:MySql使用最频繁的一个索引数据结构,数据结构以平衡树的形式来组织,因为是树型结构,所以更适合用来处理排序,范围查找等功能. (2)Hash:Hsah索引 ...
- 大数据小项目之电视收视率企业项目09--hive环境搭建
Hive是一个数据仓库基础工具在Hadoop中用来处理结构化数据.它架构在Hadoop之上,总归为大数据,并使得查询和分析方便.并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务 ...
- Special Segments of Permutation - CodeForces - 1156E (笛卡尔树上的启发式合并)
题意 给定一个全排列\(a\). 定义子区间\([l,r]\),当且仅当\(a_l + a_r = Max[l,r]\). 求\(a\)序列中子区间的个数. 题解 笛卡尔树上的启发式合并. \(200 ...
- 【Todo】 cygwin下emacs中M-x shell 中出现乱码
- 全球征集-如何实现回文SQL的查询
有个表,以下是创建的SQL: CREATE TABLE [dbo].[SysName]( ,) NOT NULL, ) COLLATE Chinese_PRC_CI_AS NULL, ) COLLAT ...
- Windows下如何用CMD命令跳转到指定的目录下
以Window7为例说明,想要跳转到I:\adt-bundle-windows-x86-20130219\sdk\platform-tools目录下. 1.在运行中打开CMD命令窗口如下图所示: 2. ...
- daily algorithm 判断链表是否有环
public static boolean isLoopLink(ListNode head) { if (head == null) { return false; } ListNode fast ...
- Android TV 开发(4)
本文来自网易云社区 作者:孙有军 最后我们再来看看好友界面,改界面本地是没有xml的,因此我们直接来看看代码: 这里将使用到数据bean,与数据源的代码也贴出来如下: public class Con ...