dp——问题转化→01背包+滚动数组

题目:

Prison rearrangement

Time Limit: 3000MS Memory Limit: 10000K
Total Submissions: 2194 Accepted: 984

 

Description

In order to lower the risk of riots and escape attempts, the boards of two nearby prisons of equal prisoner capacity, have decided to rearrange their prisoners among themselves. They want to exchange half of the prisoners of one prison, for half of the prisoners of the other. However, from the archived information of the prisoners' crime history, they know that some pairs of prisoners are dangerous to keep in the same prison, and that is why they are separated today, i.e. for every such pair of prisoners, one prisoners serves time in the first prison, and the other in the second one. The boards agree on the importance of keeping these pairs split between the prisons, which makes their rearrangement task a bit tricky. In fact, they soon find out that sometimes it is impossible to fulfil their wish of swapping half of the prisoners. Whenever this is the case, they have to settle for exchanging as close to one half of the prisoners as possible.

Input

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two non-negative integers m and r, 1 < m < 200 being the number of prisoners in each of the two prisons, and r the number of dangerous pairs among the prisoners. Then follow r lines each containing a pair xi yi of integers in the range 1 to m,which means that prisoner xi of the first prison must not be placed in the same prison as prisoner yi of the second prison.

Output

For each test scenario, output one line containing the largest integer k <= m/2 , such that it is possible to exchange k prisoners of the first prison for k prisoners of the second prison without getting two prisoners of any dangerous pair in the same prison.

Sample Input

3
101 0
3 3
1 2
1 3
1 1
8 12
1 1
1 2
1 3
1 4
2 5
3 5
4 5
5 5
6 6
7 6
8 7
8 8

Sample Output

50
0
3

题目链接:https://vjudge.net/problem/HDU-1955

分析:

通过敌对关系可以求出各个连通分量,要想某一个连通分量的一个元素进行调换,该连通分量的其他所有元素一定也会调换,因此调换操作是以连通分量为基本单位的。转化成01背包问题,为了节省空间,使用滚动数组(注意应从大到小更新)。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 410;
int n, m, r;
int l[maxn], ri[maxn];
int g[maxn][maxn];
int cnt;
bool vis[maxn];
bool dp[maxn][maxn];

void dfs(int x, int num)
{
	vis[x] = true;
	if (x <= m) l[num]++;
	else ri[num]++;
	for (int i = 1; i <= 2 * m; i++)
		if (!vis[i] && g[x][i])
			dfs(i, num);
	return;
}
int main()
{
	cin >> n;
	while (n--)
	{
		cin >> m >> r;
		int x, y;

		memset(l, 0, sizeof(l));
		memset(ri, 0, sizeof(ri));
		memset(vis, 0, sizeof(vis));
		memset(dp, 0, sizeof(dp));
		memset(g, 0, sizeof(g));
		cnt = 0;
		for (int i = 1; i <= r; i++)
		{
			cin >> x >> y;
			g[x][y + m] = g[y + m][x] = 1;
		}
		for (int i = 1; i <= 2 * m; i++)
			if (!vis[i])
				dfs(i, ++cnt);
		dp[0][0] = 1;
		for (int k = 1; k <= cnt; k++)
			for (int i = m / 2; i >= l[k]; i--)
				for (int j = m / 2; j >= ri[k]; j--)
					dp[i][j] = max(dp[i][j], dp[i - l[k]][j - ri[k]]);
		int maxi = 0;
		for (int i = 1; i <= m / 2; i++)
			if (dp[i][i])
				maxi = i;
		cout << maxi << endl;
	}

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值