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 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 ( <= N <= ) and M ( <= M <= ) 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 , , , ..., 2N - . 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 = 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
Sample Output
Source
- 题意:m个门,每个门上有两把锁,打开一个就可以通过,2n个钥匙,每两个绑在一起,只能选用一个 ,选了一个,另一个就被废弃。问最多可以通过几扇门?
- 2-sat问题关键在建图,2-sat对每个事物都有两个选项 ,选和不选. 可以这么建:
- 每把钥匙有两个状态(用或不用),把这作为2-sat的两个选项
- 然后是加条件,a、b绑在一起,则选a就不选b,选b就不选a,建边a->!b,b->!a
- c、d在同一个门上,则不开c就开d,不开d就开c,建边!c->d,!d->c
- 然后二分答案都可以了
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1<<16
#define M 1<<16
#define inf 1<<26
int n,m; ////////////////////////////////////////////////////////
int tot;
int head[N];
int vis[N];
int tt;
int scc;
stack<int>s;
int dfn[N],low[N];
int col[N];
struct Node
{
int from;
int to;
int next;
}edge[N];
void init()
{
tot=;
scc=;
tt=;
memset(head,-,sizeof(head));
memset(dfn,-,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(col,,sizeof(col));
while(!s.empty()){
s.pop();
}
}
void add(int s,int u)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].next=head[s];
head[s]=tot++;
}
void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
dfn[u] = low[u]= ++tt;
vis[u]=;
s.push(u);
int cnt=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dfn[v]==-)
{
// sum++;
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]==)
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int x;
scc++;
do{
x=s.top();
s.pop();
col[x]=scc;
vis[x]=;
}while(x!=u);
}
}
bool two_sat(){ for(int i=;i<*n*;i++){
if(dfn[i]==-){
tarjan(i);
}
}
for(int i=;i<n*;i++){
if(col[*i]==col[*i+]){
return false;
}
}
return true;
}
////////////////////////////////////////
int key1[N],key2[N];
int door1[M],door2[M]; bool solve(int mid){
init();
for(int i=;i<=n;i++){
add(*key1[i],*key2[i]+);
add(*key2[i],*key1[i]+);
} for(int i=;i<=mid;i++){
add(*door1[i]+,*door2[i]);
add(*door2[i]+,*door1[i]);
}
if(two_sat()) return true;
return false;
} int main()
{
while(scanf("%d%d",&n,&m)==){
if(n== && m==){
break;
} for(int i=;i<=n;i++){
scanf("%d%d",&key1[i],&key2[i]);
}
for(int i=;i<=m;i++){
scanf("%d%d",&door1[i],&door2[i]);
} int low=;
int high=m+;
// int ans;
while(low<high){
int mid=(low+high)>>;
if(solve(mid)){
//ans=mid;
low=mid+;
}
else{
high=mid;
}
}
printf("%d\n",low-); }
return ;
}
poj 2723 Get Luffy Out(2-sat)的更多相关文章
- HDU 1816, POJ 2723 Get Luffy Out(2-sat)
HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...
- POJ 2723 Get Luffy Out(2-SAT+二分答案)
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8851 Accepted: 3441 Des ...
- Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)
Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...
- POJ 3087 Shuffle'm Up(洗牌)
POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 A common pas ...
- POJ 1426 Find The Multiple(寻找倍数)
POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
- poj 2723 Get Luffy Out 二分+2-sat
题目链接 给n个钥匙对, 每个钥匙对里有两个钥匙, 并且只能选择一个. 有m扇门, 每个门上有两个锁, 只要打开其中一个就可以通往下一扇门. 问你最多可以打开多少个门. 对于每个钥匙对, 如果选择了其 ...
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- 【POJ】2187 Beauty Contest(旋转卡壳)
http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...
随机推荐
- HDU 1576 A/B(数论)
题目:求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1).数据给出n和b 推导过程 A/B = K K = 9973* ...
- 判断一个key 是否在map中存在
public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-gener ...
- Hadoop 写SequenceFile文件 源代码
package com.tdxx.hadoop.sequencefile; import java.io.IOException; import org.apache.hadoop.conf.Conf ...
- border-radius讲解2
一:border-radius只有一个取值时,四个角具有相同的圆角设置,其效果是一致的: .demo { border-radius: 10px; } 其等价于: .demo{ border-top- ...
- jBPM4.4与SSH2整合
整合jBPM的目的就是能够通过注入的方式得到ProcessEngine实例,因为ProcessEngine是jbpm 的核心. 整合步骤: 1.新建web程,搭建好SSH2环境 2.导入jbpm相关的 ...
- 1203.4——循环语句 之 for
for循环的一般形式为:for(表达式1; 表达式2; 表达式3){ 语句块} 它的执行过程如下:1) 先求解表达式1. 2) 求解表达式2,若其值为真(非0),则执行循环体,否则结束循环. 3 ...
- 1203.3——循环语句 之 while
while循环 while循环的一般形式为: while(表达式){ 语句块 }其中表达式称为循环条件,语句块称为循环体. while语句的意思是:先计算表达式的值,当值为真 ...
- C语言获取键盘按键
在写控制台游戏的时候,发现不管用cin,scanf还是getchar,都不能实时的输入按键,必须要按回车才能读进去,而按回车的话会导致输入异常,所以要使用获取键盘按键的函数. 加入头文件#includ ...
- virtual box 改变已经创建的虚拟系统分配的硬盘
启动cmd,进入virtualbox安装的目录 :cd E:\Program Files\Oracle\VirtualBox 然后输入VBoxManage.exe list hdds ,可以看到 D: ...
- boost vc编译
0.下载:http://www.boost.org/ 1.编译b2.exe,bjam.exe. 双击根目录下面的bootstrap.bat文件,生成b2.exe,bjam.exe(或者使用vs的命令行 ...