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 5640 King's Cake(模拟)
			
Problem Description It is the king's birthday before the military parade . The ministers prepared ...
 - pyqt 图片(label上显示
			
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from decimal import * from PyQt4.Q ...
 - hiberbate的工作原理
			
hibernate 简介:hibernate是一个开源框架,它是对象关联关系映射的框架,它对JDBC做了轻量级的封装,而我们java程序员可以使用面向对象的思想来操纵数据库.hibernate核心接口 ...
 - 页面动态数据的滚动效果——jquery滚动组件(vticker.js)
			
<script language="javascript" src="lirms/Test/jquery-1.4.2.js"></script ...
 - class、interface、struct的差别
			
1 struct和class有什么差别 1.1默认的继承訪问权限 Struct是public的,class是private的. 你能够写例如以下的代码: struct A { char a; }; s ...
 - spring + jdbc + extjs configuration
			
所有源代码能够訪问我的GitHub 有空没空的稻谷了几天,最终前后台跑通了,提供一套可用的配置文件. (因为与extjs整合,spring security的登录须要重写原handler.会在后面补上 ...
 - iOS:UI系列之UIScrollview和UIPagecontrol
			
转眼间,又是一天,就这样忙忙碌碌的一天一天的过着, 不过还好,不是浑浑噩噩的,也算是小有所成,劳有所获吧,嘿嘿! 好了,到了总结的时间啦, 下面就为大家简单讲解下我今天学习的内容吧,希望对各位都有所帮 ...
 - HTTP协议1之协议详解--转
			
当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基本协议相当重要, 因为它让我们理解了We ...
 - css之浏览器初始化方案
			
HTML, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote,pre, form, fieldset, ...
 - NFinal  ajax
			
AJAX返回 我们经常会用到ajax,当我们想返回json时,需要这样写. context.Response.ContentType="application/json"; con ...