POJ 2723 Get Luffy Out(2-SAT)

题意:你手里有2n把不同的钥匙,这2n把钥匙被分为n对,每对由两个不同的钥匙组成.现在按顺序出现了M个门,每个门上有两个锁,你只需打开其中一个锁就可以打开这个门.现在你需要用你手里的钥匙去按顺序打开门,但是对于属于同一组的两把钥匙,如果你用了钥匙A,那么以后永远不能再用钥匙B了.问你按顺序最多能打开多少个门?

思路:2-SAT


#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 10000+10;
struct TwoSAT
{
	int n;
	vector<int>G[maxn*2];
	bool mark[maxn*2];
	int S[maxn*2],c;

	void init(int n)
	{
		this->n = n;
		for (int i = 0;i<2*n;i++)
			G[i].clear();
		memset(mark,0,sizeof(mark));
	}

	void add_clause(int x,int xval,int y,int yval)
	{
		x = x*2+xval;
		y = y*2+yval;
		G[x].push_back(y);
//		G[y^1].push_back(x);
	}
	bool dfs(int x)
	{
        if (mark[x^1])
			return false;
		if (mark[x])
			return true;
		mark[x]=true;
		S[c++]=x;
		for (int i = 0;i<G[x].size();i++)
			if (!dfs(G[x][i]))
				return false;
		return true;
	}
	bool solve()
	{
		memset(mark,0,sizeof(mark));
		for (int i = 0;i<2*n;i+=2)
		{
			if (!mark[i] && !mark[i+1])
			{
				c=0;
				if (!dfs(i))
				{
					while (c>0)
						mark[S[--c]]=false;
					if (!dfs(i+1))
						return false;
				}
			}
		}
		return true;
	}
}TS;

int main()
{
    int n,m;
	while (scanf("%d%d",&n,&m)!=EOF && n)
	{
        TS.init(n*2);
		for (int i = 1;i<=n;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			TS.add_clause(a,0,b,1);
			TS.add_clause(b,0,a,1);
		//	TS.add_clause(a,1,b,0);
		//	TS.add_clause(b,1,a,0);
		}
		int i;
		for (i = 0;i<m;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			TS.add_clause(a,1,b,0);
			TS.add_clause(b,1,a,0);
			if (!TS.solve())
				break;
		}
		int ans = i;
		for (i++;i<m;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
		}
		printf("%d\n",ans);
	}
}

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值