现在的位置: 首页 > 综合 > 正文

HDU 2768 Cat vs. Dog 最大独立集

2018年05月02日 ⁄ 综合 ⁄ 共 2912字 ⁄ 字号 评论关闭

Cat vs. Dog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1588    Accepted Submission(s): 602

Problem Description
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
/*
hdoj 2768 最大独立集 

题意:有v个观众,每个人投给自己喜欢的猫(或者狗)和讨厌的狗(或者猫),
如果出现喜欢的和别人讨厌的相同,则其中一人会不满意。 
现要求得是最大满意的观众是多少。 
    
方法:把观众分成两个集合,一个是投留下猫的,一个是投留下狗的,
如果存在冲突,则在这两个观众之间加一条边
现在选择最多的顶点,要求各个顶点之间没有线相连,即不出现矛盾。
就是求最大独立集。 

1 2 4    左边猫 右边狗 
C1 D1     1 C1    4 D2
C1 D1     2 C1
C1 D2     3 C1 
D2 C1   上述中3跟4有条边 因为矛盾 所以最大的独立集3个点1 2 3都不相连   

最大独立集,在二分图G中,找出点数最多的子图,使得这些点之间都不相连。 
最大独立集=顶点数n-最大匹配 
*/
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
#define N 501
struct node{//记得加 #include<string>
	string love,hate;
};
node cat[N],dog[N];

int map[N][N],vis[N],match[N];
int c,d,v,catNum,dogNum;

int dfs(int u)
{
	int i;
	for(i=0;i<dogNum;i++)
	{
		if(map[u][i]&&!vis[i])
		{
			vis[i]=1;
			if(match[i]==-1||dfs(match[i]))
			{
				match[i]=u;
				return 1;
			}
		}
	}
	return 0;
}
int maxMatch()
{
	int i,ans=0;
	memset(match,-1,sizeof(match));
	for(i=0;i<catNum;i++)
	{
		memset(vis,0,sizeof(vis));
		if(dfs(i))
			ans++;
	}
	return ans;
	
}
int main()
{
	int t,i,j,ans;
	char str1[10],str2[10];
	
	//freopen("test.txt","r",stdin);
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&c,&d,&v);
		catNum=dogNum=0;
		for(i=0;i<v;i++)
		{
			scanf("%s",str1);
			scanf("%s",str2);
			if(str1[0]=='C')
			{
				cat[catNum].love=str1;
				cat[catNum++].hate=str2;
			}
			else
			{
				dog[dogNum].love=str1;
				dog[dogNum++].hate=str2;
			}
		}
		memset(map,0,sizeof(map));
		for(i=0;i<catNum;i++)
			for(j=0;j<dogNum;j++)
			{
				if(cat[i].love==dog[j].hate||cat[i].hate==dog[j].love)
					map[i][j]=1;
			}
		ans=maxMatch();
		printf("%d\n",v-ans);
	}
	return 0;
}

抱歉!评论已关闭.