题目

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】的更多相关文章

  1. Python开发【第一章】:Python简介和入门

    Python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种继承. ...

  2. 【WebApi系列】浅谈HTTP

    [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi如何传递参数 [04]详解WebApi测试和PostMan [05]浅谈WebApi Core ...

  3. 【WebApi系列】浅谈HTTP在WebApi开发中的运用

    WebApi系列文章 [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi参数的传递 [04]详解WebApi测试和PostMan [05]浅谈W ...

  4. 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型

    前文提要:[js实例]js中的5种基本数据类型和9种操作符 Array类型的9个数组方法 Array中有9个数组方法: 1.检测数组 2.转换方法 3.栈方法 4.队列方法 5.冲排序方法6.操作方法 ...

  5. Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】

    Spring Cloud(三):服务提供与调用 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇文章我们介绍了 Eureka 服务 ...

  6. 【插头DP】BZOJ1187- [HNOI2007]神奇游乐园

    [题目大意] 在n*m的网格中选一条回路,使权值和最大. [思路] 和之前裸的插头DP差不多,只不过现在回路不需要经过所有的格子.所以有以下几个注意点(具体看注释): (1)left和up插头相等的时 ...

  7. zz【清华NLP】图神经网络GNN论文分门别类,16大应用200+篇论文最新推荐

    [清华NLP]图神经网络GNN论文分门别类,16大应用200+篇论文最新推荐 图神经网络研究成为当前深度学习领域的热点.最近,清华大学NLP课题组Jie Zhou, Ganqu Cui, Zhengy ...

  8. 【故障解决】OGG-00446 错误解决

    [故障解决]OGG-00446 Could not find archived log for sequence 一.1  BLOG文档结构图       一.2  前言部分   一.2.1  导读和 ...

  9. 【故障解决】ORA-06502错误解决

    [故障解决]ORA-06502: PL/SQL: numeric or value error: character string buffer too small 一.1  BLOG文档结构图   ...

随机推荐

  1. HP刀片服务器安装系统步骤

    原文链接 HP刀片服务器安装系统步骤: HP刀片使用一个叫OA(Onboard Administrator)的管理平台统一进行管理,具有很高的集成性,同时使用起来也很简单方便,通过IE浏览器输入IP地 ...

  2. C# 多态与new关键字

    1. 你通常怎样用多态? 假设我有一个类,里面有一个 PrintStatus 方法,用于打印实例的当前状态,我希望该类的派生类都带有一个 PrintStatus 方法,并且这些方法都用于打印其实例的当 ...

  3. struts+hibernate+spring整合过程常见问题收集

    1.java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor缺少asm-3.3.jar2.java.lang.NoClassDefF ...

  4. java算法面试题:金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。

    package com.swift; public class RenMingBi { private static final char[] data = new char[]{'零','壹','贰 ...

  5. java 实现猜数字游戏 随机给定一个数字,猜大小直到正确

    package com.swift; import java.util.Random; import java.util.Scanner; public class GuessBigSmall { p ...

  6. IDEA注释模板设置

    1. 文件注释 1.1 Java类文件注释 Setting -> Editor -> File and Code Templates -> Includes -> File H ...

  7. SQL数据库从高版本导入低版本

    1. 打开高版本数据库右键–>任务–>生成脚本–>高级–>选择脚本兼容的版本(也就是低版本)–>拉倒最下面选择架构和数据 2. 在低版本里面,先新建一个数据库,名称要和脚 ...

  8. redis Connection refused 远程连接错误

    redis 远程连接时报错:  Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionE ...

  9. k8s资源配置清单的书写格式(yaml文件)

    yaml文件书写格式:5大类:apiVersion: 选择kubectl api-versions里面存在的版本kind: 选择kubectl api-resources结果中的对象资源metadat ...

  10. Linux产生随机数的几种方法

    .echo $RANDOM .openssl rand -base64 .date +%n%N .head /dev/urandom |cksum .cat /proc/sys/kernel/rand ...