(beginer) 二分图(最大独立集) UVA 12168 Cat vs. Dog

Problem C - Cat vs. Dog

Time limit: 2 seconds

The latest reality show has hit the TV: ``Cat vs. Dog''. In this show, a bunch of cats and dogs compete for the very prestigious Best Pet Ever title. In each episode, the cats and dogs get to show themselves off, after which the viewers vote on which pets should stay and which should be forced to leave the show.

Each viewer gets to cast a vote on two things: one pet which should be kept on the show, and one pet which should be thrown out. Also, based on the universal fact that everyone is either a cat lover (i.e. a dog hater) or a dog lover (i.e. a cat hater), it has been decided that each vote must name exactly one cat and exactly one dog.

Ingenious as they are, the producers have decided to use an advancement procedure which guarantees that as many viewers as possible will continue watching the show: the pets that get to stay will be chosen so as to maximize the number of viewers who get both their opinions satisfied. Write a program to calculate this maximum number of viewers.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

  • One line with three integers c, d, v (1 ≤ c, d ≤ 100 and 0 ≤ v ≤ 500): the number of cats, dogs, and voters.
  • v lines with two pet identifiers each. The first is the pet that this voter wants to keep, the second is the pet that this voter wants to throw out. A pet identifier starts with one of the characters `C' or `D', indicating whether the pet is a cat or dog, respectively. The remaining part of the identifier is an integer giving the number of the pet (between 1 and c for cats, and between 1 and d for dogs). So for instance, ``D42'' indicates dog number 42.

Output

Per testcase:
  • One line with the maximum possible number of satisfied voters for the show.

Sample Input

2
1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1

Sample Output

1
3
The 2008 ACM Northwestern European Programming Contest

题意:有v个观众要投票留下一只猫,抛弃一只狗或者留下一只狗,抛弃一只猫,你要尽可能满足他们的要求,问最多能满足多少人的意愿。

思路:二分图的思路感觉都好难想啊。。汗颜。。。。我们把人当做点,如果两个人的喜好有冲突,那么我们就连一条边,最后这些人可以分成两个集合的,分成喜欢狗和喜欢猫两个集合。所以是二分图,然后找最大独立集就行了。然后 最大独立集=点数-最大匹配。。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<vector>
using namespace std;
const int maxn = 510;
int a , b , m;
int link[maxn];
bool vis[maxn];
vector<int> cat[2][110];
vector<int> dog[2][110];
bool g[maxn][maxn];
vector<int> G[maxn];
vector<int> S;

void input()
{
	S.clear();
	for (int i = 0 ; i < 2 ; ++i) 
	{
		for (int j = 0 ; j < 110 ; ++j)
		{ 
			cat[i][j].clear(); 
			dog[i][j].clear(); 
		}
	}
	for (int i = 0 ; i < m ; ++i) G[i].clear();
	char buffer[20];
	char pet; int x;
	memset(g,false,sizeof(g));
	for (int i = 0 ; i < m ; ++i) {
		scanf("%s",buffer);
		sscanf(buffer,"%c%d",&pet,&x);
		if (pet=='C') {
			S.push_back(i);
			for (int j = 0 ; j < cat[1][x].size() ; ++j) {
				if (g[i][cat[1][x][j]]) continue;
				G[i].push_back(cat[1][x][j]);
				G[cat[1][x][j]].push_back(i);
				g[i][cat[1][x][j]] = g[cat[1][x][j]][i] = true;
			}
			cat[0][x].push_back(i);
		} else {
			for (int j = 0 ; j < dog[1][x].size() ; ++j) {
				if (g[i][dog[1][x][j]]) continue;
				G[i].push_back(dog[1][x][j]);
				G[dog[1][x][j]].push_back(i);
				g[i][dog[1][x][j]] = g[dog[1][x][j]][i] = true;
			}
			dog[0][x].push_back(i);
		}
		scanf("%s",buffer);
		sscanf(buffer,"%c%d",&pet,&x);
		if (pet=='D') {
			for (int j = 0 ; j < dog[0][x].size() ; ++j) {
				int y = dog[0][x][j];
				if (g[i][y]) continue;
				G[i].push_back(y);
				G[y].push_back(i);
				g[i][y] = g[y][i] = true;
			}
			dog[1][x].push_back(i);
		} else {
			for (int j = 0 ; j < cat[0][x].size() ; ++j) {
				int y = cat[0][x][j];
				if (g[i][y]) continue;
				G[i].push_back(y);
				G[y].push_back(i);
				g[i][y] = g[y][i] = true;
			}
			cat[1][x].push_back(i);
		}
	}
}

bool dfs(int x)
{
	if (vis[x]) return false;
	vis[x] = true;
	for (int i = 0 ; i < G[x].size() ; ++i) {
		int y = G[x][i];
		if (vis[y]) continue;
		if (link[y]==-1 || dfs(link[y])) {
			link[y] = x;
			return true;
		}
	}
	return false;
}

void solve()
{
	memset(link,-1,sizeof(link));
	int ans = m;
	for (int i = 0 ; i < S.size() ; ++i) 
	{
		memset(vis,0,sizeof(vis));
		if (dfs(S[i])) --ans;
	}
	printf("%d\n",ans);
}

int main()
{
	int T; cin>>T;
	while (T--)
	{
		scanf("%d%d%d",&a,&b,&m);
		input();
		solve();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值