您的位置:首页 > 其它

【HDU3829】【二分匹配】【最小独立集】【问啥建啥】

2015-09-04 10:41 423 查看


Cat VS Dog

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)

Total Submission(s): 3117 Accepted Submission(s): 1086



Problem Description

The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child's like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa.

Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.



Input

The input file contains multiple test cases, for each case, the first line contains three integers N <= 100, M <= 100 and P <= 500.

Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)



Output

For each case, output a single integer: the maximum number of happy children.



Sample Input

1 1 2
C1 D1
D1 C1

1 2 4
C1 D1
C1 D1
C1 D2
D2 C1




Sample Output

1
3

HintCase 2: Remove D1 and D2, that makes child 1, 2, 3 happy.




Source

2011 Multi-University Training Contest 1 - Host
by HNU



Recommend

xubiao

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
   
const int N = 550;
int g

;
int used
;
int uN,vN;
int match
;
bool dfs(int u)  
{  
    for(int i=1;i<=vN;i++)  
    {  
        if(!used[i] && g[u][i])  
        {  
            used[i] = true;  
            if(match[i] == -1 || dfs(match[i]))  
            {  
                match[i] = u;  
                return true;  
            }  
        }  
    }  
    return false;  
}  
int hungary()  
{  
    int ret = 0;  
    for(int i=1;i<=uN;i++)     
    {  
        memset(used,0,sizeof(used));  
        if(dfs(i))  ret++;  
    }  
    return ret;
}

int n,m,p;
struct Node
{
	char s1[5],s2[5];
	void input()
	{
		scanf("%s%s",s1,s2);
	}
}node[550];
int main()
{
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)
	{
		memset(match,-1,sizeof(match));
		memset(g,0,sizeof(g));
		for(int i=1;i<=p;i++)
		{
			node[i].input();
		}
		for(int i=1;i<=p;i++)
		{
			for(int j=1;j<=p;j++)
			{
				if(strcmp(node[i].s1,node[j].s2) == 0 || strcmp(node[i].s2,node[j].s1) == 0)
				{
					g[i][j] = 1;
					g[j][i] = 1;
				}
			}
		}
		vN = p;
		uN = p;
		printf("%d\n",p-hungary()/2);
	}	
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: