HDU2768 Cat vs. Dog

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
http://acm.split.hdu.edu.cn/showproblem.php?pid=2768

2种动物一种猫C 狗D   V个人 每个人自己都喜欢一种动物的某一个 讨厌另一种动物的某一个

怎么选 能满足尽量多的人 输出最大人数

2组数据  1个猫 一个狗 2个人

第一个C1 D1 第二D1 C1

最多只能满足一个人


把人当做二分图的点 然后有矛盾的点可以相连(多想一下)

/*
    题意:有v个观众,每个人投给自己喜欢的猫(或者狗)和讨厌的狗(或者猫),如果出现喜欢的和别人讨厌的相同,则其中一人会不满意。
现要求得是最大满意的观众是多少。
    方法:根据出现矛盾的两个观众序号建边。现在选择最多的顶点,要求各个顶点之间没有线相连,即不出现矛盾。就是求最大独立集。
    
    最大匹配:二分图G中,找出边数最大的子图M,使得M中各条边均无公共顶点,则M为最大匹配。可用匈牙利算法求得。
    最大独立集,在二分图G中,找出点数最多的子图,使得M中各点之间都不相连。
    最大独立集=顶点数n-最大匹配
*/

#include<bits/stdc++.h>
using namespace std;
int match[610];
int ap[610][610];
int vis[610];
int m,n,k;
int dfs(int u)
{
    int i;
    for(i=0; i<k; i++)
    {
        if(ap[u][i]==1&&!vis[i])
        {
            vis[i]=1;
            if(match[i]==-1||dfs(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int i,sum,t,j;
    string str[600][10];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        memset(match,-1,sizeof(match));
        memset(ap,-1,sizeof(ap));
        for(i=0; i<k; i++)
        {
            cin>>str[i][0]>>str[i][1];
            for(j=0; j<i; j++)
                if(str[j][0]==str[i][1]||str[i][0]==str[j][1])
                {
                    ap[i][j]=1;
                    ap[j][i]=1;
                }
        }
        sum=0;
        for(i=0; i<k; i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i)) sum++;
        }
        printf("%d\n",k-sum/2);
    }
    return 0;
}
除2


http://blog.csdn.net/a601025382s/article/details/11488783

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=505;
vector<int>e[maxn];
int vis[maxn],pre[maxn];
int find(int u)//判断增广路是否存在,匈牙利算法
{
    int i,j,v;
    for(i=0;i<e[u].size();i++)
    {
        v=e[u][i];
        if(!vis[v])
        {
            vis[v]=1;
            if(pre[v]==-1||find(pre[v]))
            {
                pre[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
string a[maxn],b[maxn];
int main()
{
    int c,d,v,T;
    cin>>T;
    while(T--)
    {
        cin>>c>>d>>v;
        int i,j,k;
        for(i=1;i<=v;i++)
            e[i].clear();
        for(i=1;i<=v;i++)
        cin>>a[i]>>b[i];
        for(i=1;i<=v;i++)
            for(j=1;j<=v;j++)
            if(a[i]==b[j]||b[i]==a[j])
            {
                e[i].push_back(j);
                e[j].push_back(i);
            }
        memset(pre,-1,sizeof(pre));
        int ans=0;
        for(i=1;i<=v;i++)
        {
            memset(vis,0,sizeof(vis));
            ans+=find(i);
        }
        cout<<v-ans/2<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值