Cat vs. Dog

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

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
 
 
思路:

以cat_lover和dog_lover把观众分为两个集合。只要两个集合内的人的选择有冲突,这两个顶点连接,边代表矛盾,然后求最大独立集。

最大独立集 = 顶点数 - 最小顶点覆盖数(最大匹配数)

  题目中告诉我们喜欢狗的一定不喜欢猫,喜欢猫的人一定不喜欢狗,那么这样我们就把观众分为了两类,一类是喜欢猫的,另一类是喜欢狗的,我们知道这两种观众不可能同时满足,这个时候我们要满足最多的人,这个时候就转化成了最大独立集。
    这个时候我们构的图为以喜欢的动物为一个集合,然后将喜欢的动物与讨厌的动物相同(即发生矛盾)的人连线,然后跑二分图匹配。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 510
using namespace std;
char ch1,ch2;
bool vis[N];
int t,x,y,n,m,k,ans,sdog,scat,girl[N],map[N][N];
struct Cat
{
    int like,hate;
}cat[N];
struct Dog
{
    int like,hate;
}dog[N];
int find(int x)
{
    ;i<=sdog;i++)
    {
        if(!vis[i]&&map[x][i])
        {
            vis[i]=true;
            ||find(girl[i])) {girl[i]=x; ;}
        }
    }
    ;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        ans=;sdog=,scat=;
        memset(map,,sizeof(map));
        ;i<=k;i++)
        {
            getchar();//读入换行
            scanf("%c%d",&ch1,&x);
            getchar();//读入空格
            scanf("%c%d",&ch2,&y);
            if(ch1=='C')
            {
                cat[++scat].like=x;
                cat[scat].hate=y;
            }
            else
            {
                dog[++sdog].like=x;
                dog[sdog].hate=y;
            }
        }
        ;i<=scat;i++)
         ;j<=sdog;j++)
          if(cat[i].like==dog[j].hate||cat[i].hate==dog[j].like)
           map[i][j]=;
        memset(girl,-,sizeof(girl));
        ;i<=scat;i++)
        {
            memset(vis,,sizeof(vis));
            if(find(i)) ans++;
        }
        printf("%d\n",k-ans);
    }
    ;
}

HDU——2768 Cat vs. Dog的更多相关文章

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

    Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu 2768 Cat vs. Dog 最大独立集 巧妙的建图

    题目分析: 一个人要不是爱狗讨厌猫的人,要不就是爱猫讨厌狗的人.一个人喜欢的动物如果离开,那么他也将离开.问最多留下多少人. 思路: 爱猫和爱狗的人是两个独立的集合.若两个人喜欢和讨厌的动物是一样的, ...

  3. HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配)

    HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配) Description The zoo have N cats and M dogs, toda ...

  4. HDU 3829——Cat VS Dog——————【最大独立集】

    Cat VS Dog Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  5. HDU 3289 Cat VS Dog (二分匹配 求 最大独立集)

    题意:每个人有喜欢的猫和不喜欢的狗.留下他喜欢的猫他就高心,否则不高心.问最后最多有几个人高心. 思路:二分图求最大匹配 #include<cstdio> #include<cstr ...

  6. hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Prob ...

  7. hdu 3829 Cat VS Dog 二分匹配 最大独立点集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3829 题目大意: 给定N个猫,M个狗,P个小朋友,每个小朋友都有喜欢或者不喜欢的某猫或者某狗 管理员从 ...

  8. HDU 3829 - Cat VS Dog (二分图最大独立集)

    题意:动物园有n只猫和m条狗,现在有p个小孩,他们有的喜欢猫,有的喜欢狗,其中喜欢猫的一定不喜欢狗,喜欢狗的一定不喜欢猫.现在管理员要从动物园中移除一些动物,如果一个小孩喜欢的动物留了下来而不喜欢的动 ...

  9. HDU 3829 Cat VS Dog(最大独立集)

    题目大意: 有n只猫,有m只狗.现在有P个学生去参观动物园.每个孩子有喜欢的动物和不喜欢的动物.假如他喜欢猫那么他就一定不喜欢狗(反之亦然). 如果一个孩子喜欢一个动物,那么这个动物不会被移除,若是不 ...

随机推荐

  1. word打印小册子

    使用联想m7250f打印册子,打印时设置该打印机属性为双面打印(手动),打印第一面后,将所有打印出的纸拿出并翻转使对应word中的第2页的打印纸朝外,之后将所有纸放入纸盒,再点击打印第二面即可.

  2. PHP一句话后门过狗姿势万千之传输层加工

    既然木马已就绪,那么想要利用木马,必然有一个数据传输的过程,数据提交是必须的,数据返回一般也会有的,除非执行特殊命令. 当我们用普通菜刀连接后门时,数据时如何提交的,狗狗又是如何识别的,下面结合一个实 ...

  3. About the iOS File System

    两个维度: 1)是否给用户使用: 2)是否持久存储. During installation of a new app, the installer creates a number of conta ...

  4. Unity复杂的旋转-欧拉角和四元数

    一.欧拉角欧拉角最容易表示,用三个变量X,Y,Z可以直观的表示绕着某个轴的旋转角度. 在Unity里就是Transform组件的Rotation里的X Y Z三个变量代表了欧拉角 二.四元数四元数相比 ...

  5. 在Vue中遇到的各种坑 及性能提升

    Vue: (1)    没有再模板里引用data数据,会不会引起update.beforeUpdate生命周期函数的执行? 不会 (2)组件改成异步 (3)v-once (4)如果不用template ...

  6. Perl: hash散列转换为Json报错集, perl.c,v $$Revision: 4.0.1.8 $$Date: 1993/02/05 19:39:30 $

    bash-2.03$ ./u_json.pl Can't locate object method "encode" via package "JSON" at ...

  7. idea创建Maven项目时Maven插件内看不到mybatis-generator

    创建Maven项目时插件配置添加了mybatis-generator但是右侧maven project始终没有看到插件 需要放在和pluginManagement同级别,修改配置如下:

  8. JavaScript递归简单实现个对象深拷贝

    JavaScript中对象的深拷贝来说一直都算比较恶心 毕竟没有什么api能直接全拷贝了 得自己便利写  最近在项目中需要深拷贝 自己简单封了个方法 话不多说 直接上码 <!DOCTYPE ht ...

  9. 计蒜客 Overlapping Rectangles (离散化)

    题意: 给定一个坐标系, 给出n个矩形的左下角坐标(bx,by)和右上角坐标(tx,ty) , 求矩形覆盖的面积, 有些区域会被多个矩形覆盖, 但只用算一次. n <= 1000,  0 < ...

  10. HDU 6216 A Cubic number and A Cubic Number(数学/二分查找)

    题意: 给定一个素数p(p <= 1e12),问是否存在一对立方差等于p. 分析: 根据平方差公式: 因为p是一个素数, 所以只能拆分成 1*p, 所以 a-b = 1. 然后代入a = b + ...