hdu 2768 Cat vs. Dog (二分匹配)
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
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.
* 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.
* One line with the maximum possible number of satisfied voters for the show.
好好地一题最大独立集被我毁了...
开始思路匹配错了.后来才想清楚,人应该和人匹配,构图原则是一个人喜欢的和另一个人不喜欢的一样则两者匹配,最后求最大独立集。
最大独立集=原点数-最大匹配/2(构图时构双向图);
//93MS 1780K 1160 B C++
#include<iostream>
#include<vector>
#include<string.h>
#define N 505
using namespace std;
int match[N];
int vis[N];
int n,m,k;
vector<int>V[N];
int dfs(int u)
{
for(int i=;i<V[u].size();i++){
int v=V[u][i];
if(!vis[v]){
vis[v]=;
if(match[v]==- || dfs(match[v])){
match[v]=u;
return ;
}
}
}
return ;
}
int hungary()
{
int ret=;
memset(match,-,sizeof(match));
for(int i=;i<k;i++){
memset(vis,,sizeof(vis));
ret+=dfs(i);
}
return ret;
}
int main(void)
{
char a[N][],b[N][];
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++) V[i].clear();
for(int i=;i<k;i++)
scanf("%s%s",&a[i],&b[i]);
for(int i=;i<k;i++)
for(int j=;j<k;j++){
if(strcmp(a[i],b[j])== || strcmp(a[j],b[i])==){
V[i].push_back(j);
V[j].push_back(i);
}
}
printf("%d\n",k-hungary()/);
}
return ;
}
最新文章
- 发布有礼!2015 Autodesk程序商店有奖发布活动拉开序幕
- MYSQL的常用命令和增删改查语句和数据类型【转】
- miniprofiler的对数据库的监测使用。以nancy,petapoco为例
- poj 2031Building a Space Station(几何判断+Kruskal最小生成树)
- hdu1018
- 自定义JSTL标签和函数库
- 【Avalon】escape
- PHP包名解释
- sql连接字符串的方法
- HDU 1677
- 面试题总结之JAVA
- C#依据word模版动态生成文档
- Servlet中文乱码问题解决办法
- Windows Server 2016-Powershell加域并指定OU (二)
- freekan5.9电影网站安装及源码分享
- 正则re模块
- javascript 小方块平移
- 本地搭建ELK(elasticsearch, logstash, kibana)日志收集系统
- webpack 打包调试
- 摘记—Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha