您的位置:首页 > 其它

hdu 2768 Cat vs. Dog (二分匹配)

2014-05-28 19:06 423 查看

Cat vs. Dog

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
Per testcase:

* One line with the maximum possible number of satisfied voters for the show.

[align=left]Sample Input[/align]

2

1 1 2

C1 D1
D1 C1

1 2 4

C1 D1

C1 D1
C1 D2
D2 C1

[align=left]Sample Output[/align]

1

3

[align=left]Source[/align]
NWERC 2008

[align=left]Recommend[/align]
lcy | We have carefully selected several similar problems for you: 2767 2769 2772 2770 2773

好好地一题最大独立集被我毁了...

开始思路匹配错了.后来才想清楚,人应该和人匹配,构图原则是一个人喜欢的和另一个人不喜欢的一样则两者匹配,最后求最大独立集。

最大独立集=原点数-最大匹配/2(构图时构双向图);

//93MS    1780K    1160 B    C++
#include<iostream>
#include<vector>
#include<string.h>
#define N 505
using namespace std;
int match
;
int vis
;
int n,m,k;
vector<int>V
;
int dfs(int u)
{
for(int i=0;i<V[u].size();i++){
int v=V[u][i];
if(!vis[v]){
vis[v]=1;
if(match[v]==-1 || dfs(match[v])){
match[v]=u;
return 1;
}
}
}
return 0;
}
int hungary()
{
int ret=0;
memset(match,-1,sizeof(match));
for(int i=0;i<k;i++){
memset(vis,0,sizeof(vis));
ret+=dfs(i);
}
return ret;
}
int main(void)
{
char a
[5],b
[5];
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<=k;i++) V[i].clear();
for(int i=0;i<k;i++)
scanf("%s%s",&a[i],&b[i]);
for(int i=0;i<k;i++)
for(int j=0;j<k;j++){
if(strcmp(a[i],b[j])==0 || strcmp(a[j],b[i])==0){
V[i].push_back(j);
V[j].push_back(i);
}
}
printf("%d\n",k-hungary()/2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: