Get Luffy Out * (2-sat + 二分)

Get Luffy Out *

Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Problem 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 made N pairs,one key may be appear in some 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 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
 

//题意:有n对钥匙(每对钥匙可以看成是一样的,比如1  4是一对钥匙,4可以开1的锁,1也可以开4的锁),共2n把,每对钥匙用了其中一把,另一把会消失,要用这些钥匙去开门,每扇门上有2个钥匙孔,只要其中一个钥匙就能把门打开(当然也可以用2把),要求门开的越多越好,还有,门必须按照他给的一扇一扇开下去,不能跳着开。

//思路:这题的一个point是要自己先假设能开多少扇门,再用2-sat去验证(2-sat这个算法是来找矛盾的),怎么假设呢?在[0,m](m是总的门数)之间进行二分,每次取mid代入2-sat去验证,如果这个数量的门是可以开的,到[mid+1,m]在进行二分,反之,若开不了这个数量的门,到[0,mid-1]进行二分,直到 l>r 了,说明找到值了。

还有一个point是2-sat里如何建图:因为是2n把钥匙,所以我们要拆成4n个点(1,1*,2,2*...)(如果不知道为什么要拆点的话建议先去看下2-sat算法介绍)。

注:x是用这把钥匙,x*是不用这把钥匙。

然后先根据配对的钥匙建图,如果u和v是一对钥匙,那我们建边:(u,v*),(v,u*)  因为题目说每对钥匙用了其中一把,另一把会消失,所以用了u就不能用v了,用了v就不能用u。

再根据每扇门上的2个钥匙孔(假设是u,v)建边:(u*,v),(v*,u)   注意:这里跟上面不一样,建的是反边。
为什么要建反边:因为这里不是用了u开了门就不能用v再来开这扇门,仔细看下题目,也是可以2把同时开的,这里要的应该是如果要开这扇门,如果不用u就一定要用v,反之,如果不用v就一定要用u,所以建的是反边。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 4010;

typedef struct {
	int x, y;
}Pair;

int top;
int n, m;
Pair pairs[MAX];
vector<int>map[MAX];
bool mark[MAX];
int S[MAX], lock1[MAX], lock2[MAX];

void add(int x, int valx, int y, int valy)
{
	x = x * 2 + valx;
	y = y * 2 + valy;
	map[x ^ 1].push_back(y);
	map[y ^ 1].push_back(x);
}

bool dfs(int x)
{
	if (mark[x ^ 1])
		return false;
	if (mark[x])
		return true;
	mark[x] = true;
	S[++top] = x;
	for (int i = 0; i < map[x].size(); i++)
	{
		if (!dfs(map[x][i]))
			return false;
	}
	return true;
}

bool two_sat(int mid)
{
	//建边
	for (int i = 0; i < n * 4; i++)
		map[i].clear();
	for (int i = 0; i < n; i++)
		add(pairs[i].x, 0, pairs[i].y, 0);
	for (int i = 1; i <= mid; i++)
		add(lock1[i], 1, lock2[i], 1);
	memset(mark, 0, sizeof(mark));

	//2-sat算法
	for (int i = 0; i < 4 * n; i++)
	{
		if (!mark[i] && !mark[i ^ 1])
		{
			top = 0;
			if (!dfs(i))
			{
				while (top)
					mark[S[top--]] = false;
				if (!dfs(i ^ 1))
					return false;
			}
		}
	}
	return true;
}

void slove()
{
	int l = 1, r = m, mid;
	while (l <= r)
	{
		mid = (l + r) / 2;
		//可以开这个数量的门
		if (two_sat(mid))
			l = mid + 1;
		//开不了这个数量的门
		else
			r = mid - 1;
	}
	printf("%d\n", l - 1);
}

int main()
{
	while (scanf("%d%d", &n, &m), n || m)
	{
		int a, b;
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d", &a, &b);
			pairs[i].x = a;
			pairs[i].y = b;
		}
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d", &a, &b);
			lock1[i] = a;
			lock2[i] = b;
		}
		slove();
	}
	return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值