Cat VS Dog

Problem Description
The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child's like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa.
Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.
 
Input
The input file contains multiple test cases, for each case, the first line contains three integers N <= 100, M <= 100 and P <= 500.
Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)
 
Output
For each case, output a single integer: the maximum number of happy children.
 
Sample Input
1 1 2
C1 D1
D1 C1

1 2 4
C1 D1
C1 D1
C1 D2
D2 C1

 
Sample Output
1
3

Hint

Case 2: Remove D1 and D2, that makes child 1, 2, 3 happy.

 
Source

————————————————————————————————

题目的意思是有n个人,每个人有喜欢的动物和讨厌的动物,如果保留他喜欢的删去讨厌的他就很高兴,问最多让多少人高兴

思路:根据人喜恶互斥关系建图,然后二分图最大匹配求最大独立点集

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
const int MAXN=1005;
int uN,vN,n; //u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
int link[MAXN];
int vis[MAXN];
bool dfs(int u)
{
int v;
for(v=0; v<vN; v++)
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
return false;
} int hungary()
{
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=0; u<uN; u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
} int main()
{
int m,k,x,y,T;
string s1[1005],s2[1005];
while(~scanf("%d%d%d",&x,&y,&m))
{ memset(g,0,sizeof g);
for(int i=0; i<m; i++)
{
cin>>s1[i]>>s2[i];
}
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
{
if(s1[i]==s2[j]||s2[i]==s1[j])
g[i][j]=1;
}
uN=vN=m;
printf("%d\n",m-hungary()/2);
}
return 0;
}

  

Hdu3829 Cat VS Dog(最大独立点集)的更多相关文章

  1. HDU3829 Cat VS Dog —— 最大独立集

    题目链接:https://vjudge.net/problem/HDU-3829 Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  2. HDU3829 Cat VS Dog

    题目链接:https://vjudge.net/problem/HDU-3829 题目大意: 有\(P\)个小孩,\(N\)只猫,\(M\)只狗.每个小孩都有自己喜欢的某一只宠物和讨厌的某一只宠物(其 ...

  3. HDU3829:Cat VS Dog(最大独立集)

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

  4. 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 ...

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

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

  6. Cat VS Dog

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

  7. POJ Girls and Boys (最大独立点集)

                                                                Girls and Boys Time Limit: 5000MS   Memo ...

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

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

  9. (hdu step 6.3.7)Cat vs. Dog(当施工方规则:建边当观众和其他观众最喜爱的东西冲突,求最大独立集)

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

随机推荐

  1. c# 上传excel数据总结(一)线程的使用

    1: 因为程序涉及到上传,开始暂停,继续,删除, thread 在老版本用使用th.Abort(); th.Resume(); 停止 恢复  th.Suspend(); 挂起 猛的一看挺合适啊..但微 ...

  2. linux 发送Post请求 json格式

    curl -H "Content-type: application/json" -X POST -d '{"text":"总体来说很不错,环境挺好的 ...

  3. C++树的插入和遍历(关于指针的指针,指针的引用的思考)

    题目 写一个树的插入和遍历的算法,插入时按照单词的字典顺序排序(左边放比它"小"的单词,右边放比它"大"的单词),对重复插入的单词进行计数. 程序源码 #inc ...

  4. Javascript的一个怪现象

    javascript有一个怪现象,就是减法也会导致小数位数问题,是一个麻烦的问题,比如. <html><script> var a=10,b=20.1; alert( a - ...

  5. Creating Your Own PHP Helper Functions In Laravel

    By Hamza Ali LAST UPDATED AUG 26, 2018  12,669 104 Laravel provides us with many built-in helper fun ...

  6. oracle 62进制序列号

    create or replace function GetSerial62(v_lpad number default 0) return varchar2 IS v_tmp number(38,0 ...

  7. Elastix 安装G729 G723语音编码

    下載適合自己機器及軟體版本的模組檔,基本上略分為 pentium/pentium2/pentium3/x86_64,Asterisk 1.2/1.4/1.6.前往 http://asterisk.ho ...

  8. CRC标准以及简记式

    一.CRC标准 下表中列出了一些见于标准的CRC资料: 名称 生成多项式 简记式* 应用举例 CRC-4 x4+x+1 3 ITU G.704 CRC-8 x8+x5+x4+1 31 DS18B20 ...

  9. 24、JSON与OC互相转化

    一. JSON: 1. 01.JSON是一种轻量级的数据格式,一般用于数据交互 02.服务器返回给客户端的数据,一般都是JSON格式活着XML格式(文件下载除外) JSON的格式很像OC中的字典和数组 ...

  10. 必看的经典金融书籍推荐zz

    5. 现代企业财务管理,11th詹姆斯.C.范霍恩,经济科学出版社,2002 6. Financial market and corporate strategy,glinbratt, 四.金融计量 ...