您的位置:首页 > 其它

HDU 2768 Cat vs. Dog 【二分匹配之最大独立集】

2016-10-12 21:02 363 查看


Cat vs. Dog

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

Total Submission(s): 2088    Accepted Submission(s): 804


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

 

Source

NWERC 2008



原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2768

最大独立集=顶点数n-最大匹配 

图论的题目,关键还是建图!

题意:有v个观众,每个人投给自己喜欢的猫(或者狗)和讨厌的狗(或者猫),如果出现喜欢的和别人讨厌的相同,则其中一人会不满意。 

现要求得是最大满意的观众是多少。 

    方法:根据出现矛盾的两个观众序号建边。现在选择最多的顶点,要求各个顶点之间没有线相连,即不出现矛盾。就是求最大独立集。 

    最大匹配:二分图G中,找出边数最大的子图M,使得M中各条边均无公共顶点,则M为最大匹配。可用匈牙利算法求得。 

    最大独立集,在二分图G中,找出点数最多的子图,使得M中各点之间都不相连。 

    最大独立集=顶点数n-最大匹配 

参考博客链接:http://blog.csdn.net/a601025382s/article/details/11488783

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int maxn=500+5;
int n,m,cnt;
vector<int>G[maxn];
bool vis[maxn];
int link[maxn];
string a[maxn],b[maxn];
bool Find(int x)
{
for(int i=0;i<G[x].size();i++)
{
int v=G[x][i];
if(!vis[v])
{
vis[v]=true;
if(link[v]==-1||Find(link[v]))
{
link[v]=x;
return true;
}
}
}
return false;
}
int Hungery()
{
int ans=0;
for(int i=0;i<cnt;i++)
{
memset(vis,false,sizeof(vis));
if(Find(i))
ans++;
}
return ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m>>cnt;
for(int i=0;i<cnt;i++)
G[i].clear();
for(int i=0;i<cnt;i++)
{
cin>>a[i]>>b[i];
}
for(int i=0;i<cnt;i++)
{
for(int j=0;j<cnt;j++)
{
//矛盾
if(a[i]==b[j]||b[i]==a[j])
G[i].push_back(j);
}
}
memset(link,-1,sizeof(link));
cout<<cnt-Hungery()/2<<endl;
}
return 0;
}


尊重原创,转载请注明出处:http://blog.csdn.net/hurmishine
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息