POJ 2723 Get Luffy Out

30 篇文章 0 订阅
2 篇文章 0 订阅

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 (1 <= N <= 2 10) and M (1 <= M <= 2 11) 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.

Output

For each test case, output one line containing an integer, which is the maximum number of doors Ratish can open.

Sample Input

3 6
0 3
1 2
4 5
0 1
0 2
4 1
4 2
3 5
2 2
0 0

Sample Output

4

Source

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2-SAT+二分~

T了好半天,突然意识到是fi没有清零。

我们二分答案,然后把小于等于mid的门都加上限制,用2-SAT判断是否可行即可。


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

int n,m,fi[5000],w[100000],ne[100000],cnt,dfn[5000],low[5000],bel[5000];
int x[100000],y[100000],l,r,mid,xx,yy,fii[5000],cntt,tot,q[5000],top,ans;
bool b[5000];

int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

void add(int u,int v)
{
	w[++cnt]=v;ne[cnt]=fi[u];fi[u]=cnt;
}

void dfs(int u)
{
	dfn[u]=low[u]=++cnt;q[++top]=u;b[u]=1;
	for(int i=fi[u];i;i=ne[i])
	  if(!dfn[w[i]])
	  {
	  	dfs(w[i]);
	  	low[u]=min(low[u],low[w[i]]);
	  }
	  else if(b[w[i]]) low[u]=min(low[u],dfn[w[i]]);
	if(dfn[u]==low[u])
	{
		int now=0;tot++;
		while(now!=u)
		{
			now=q[top--];
			b[now]=0;
			bel[now]=tot;
		}
	}
}

bool che(int u)
{
	memcpy(fi,fii,sizeof(fi));cnt=cntt;tot=0;
	memset(dfn,0,sizeof(dfn));
	for(int i=1;i<=u;i++) add(x[i]+n*2,y[i]),add(y[i]+n*2,x[i]);
	cnt=0;
	for(int i=1;i<=4*n;i++) if(!dfn[i]) dfs(i);
	for(int i=1;i<=n*2;i++) if(bel[i]==bel[i+n*2]) return 0;
	return 1;
}

int main()
{
	while(n=read())
	{
		memset(fi,0,sizeof(fi));cnt=0;
		m=read();
		for(int i=1;i<=n;i++) xx=read()+1,yy=read()+1,add(xx,yy+n*2),add(yy,xx+n*2);
		memcpy(fii,fi,sizeof(fi));cntt=cnt;
		for(int i=1;i<=m;i++) x[i]=read()+1,y[i]=read()+1;
		l=0;r=m;ans=0;
		while(l<=r)
		{
			mid=l+r>>1;
			if(che(mid)) ans=mid,l=mid+1;
			else r=mid-1;
		}
		printf("%d\n",ans);
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值