poj 2723 Get Luffy Out 2-sat

题意:

有2*n把钥匙配成n对,每对中只能使用一把,另外有m道门,每道门能被2把药匙打开,问最多能从1开始按顺序打开多少道门。

分析:

二分枚举能打开的门数,用2-sat算法判断能否打开。

代码:

//poj 2723
//sep9
#include <iostream>
#include <cstdio>
#include <string.h>
#include <stack>
using namespace std;
const int maxN=10024;
const int maxM=100024;
int e,n,m,t,ecnt;
int head[maxN],ins[maxN],low[maxN],dfn[maxN];
int sol[maxN],belong[maxN];
stack<int> s;
struct Edge
{
	int v,next;
}edge[maxM];
struct Door
{
	int x,y;
}door[maxM];
struct Lock
{
	int x,y;
}lock[maxN];
void addegde(int u,int v)
{
	edge[e].v=v;edge[e].next=head[u];
	head[u]=e++;
}

void dfs(int x)
{
	low[x]=dfn[x]=++t;
	s.push(x);
	ins[x]=1;
	for(int i=head[x];i!=-1;i=edge[i].next){
		int v=edge[i].v;
		if(!dfn[v]){
			dfs(v);
			low[x]=min(low[x],low[v]);
		}else if(ins[v]==1)
			low[x]=min(low[x],dfn[v]);
	}
	if(dfn[x]==low[x]){
		++ecnt;
		int k;
		do{
			k=s.top();
			s.pop();
			ins[k]=0;
			belong[k]=ecnt;
		}while(dfn[k]!=low[k]);
	}
}
int two_sat()
{
	memset(ins,0,sizeof(ins));
	memset(dfn,0,sizeof(dfn));
	while(!s.empty()) s.pop();
	int i;
	t=0,ecnt=0;
	for(i=1;i<=2*n;++i)
		if(!dfn[i])
			dfs(i);
	for(i=1;i<=n;++i)
		if(belong[i]==belong[i+n])
			return 0;
	return 1;
}

int pass(int mid)
{
	if(mid==0)
		return 1;
	int i;
	e=0;
	memset(head,-1,sizeof(head));
	for(i=1;i<=n;++i){
		int a=lock[i].x;
		int b=lock[i].y;
		addegde(a,b+n);
		addegde(b,a+n);
	}

	for(i=1;i<=mid;++i){
		int a=door[i].x;
		int b=door[i].y;
		addegde(a+n,b);
		addegde(b+n,a);
	}
	return two_sat();
}

int main()
{
	while(scanf("%d%d",&n,&m)==2){
		if(m==0&&n==0)
			break;
		int i;
		for(i=1;i<=n;++i){
			scanf("%d%d",&lock[i].x,&lock[i].y);
			++lock[i].x;
			++lock[i].y;
		}
		n*=2;
 		for(i=1;i<=m;++i){
 			scanf("%d%d",&door[i].x,&door[i].y);
 			++door[i].x;
 			++door[i].y;
		 }
		int l=0,r=m+1;
		while(l<r){
			int mid=(r+l)/2;
			if(pass(mid))
				l=mid+1;
			else
				r=mid;
		}
		printf("%d\n",l-1);
	}
	return 0;
}
时间: 2024-11-09 05:53:17

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="">题目链接 题意:N串钥匙.每串2把,仅仅能选一把.然后有n个大门,每一个门有两个锁,开了一个就能通过,问选一些钥匙,最多能通过多少个门 思路:二分通过个数.然后对于钥匙建边至少一个不选,门建边至少一个选,然后2-sat搞一下就可以. 一開始是按每串钥匙为1个结点,但是后面发现数据有可能一把钥匙,出如今不同串(真是不合

POJ 2723 Get Luffy Out(图论-2SAT,搜索-二分)

Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7488   Accepted: 2845 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 Arlo

POJ 2723 Get Luffy Out(2-SAT)

[题目链接] http://poj.org/problem?id=2723 [题目大意] 给出一些钥匙和M扇有顺序的门,每扇门可以用两种钥匙打开, 每两把钥匙被绑在一起,绑在一起的钥匙只有其中一把可以使用, 问最多能按顺序打开几扇门. [题解] 因为门是按顺序的,因此能打开的门是单调, 首先我们二分这个答案,判定是否可行, 将二分得到的答案之前的门按两个钥匙孔拆分成两个点, 两个绑在一起的钥匙属于对立面,而门的两个钥匙孔是OR关系 A与B之间的OR关系,我们可以将其拆分为!A->B AND !B

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

poj 2723

Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7295   Accepted: 2778 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 Arlo

Get Luffy Out (poj 2723 二分+2-SAT)

Language: Default Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7969   Accepted: 3061 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 o

(2 sat) poj 2723

Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7758   Accepted: 2969 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 Arlo

【图论补完计划】poj 2723(2-SAT)

Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8688   Accepted: 3371 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 Arlo

图论常用算法之一 POJ图论题集【转载】

POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:http://poj.org/ 1062* 昂贵的聘礼 枚举等级限制+dijkstra 1087* A Plug for UNIX 2分匹配 1094 Sorting It All Out floyd 或 拓扑 1112* Team Them Up! 2分图染色+DP 1125 Stockbroker