Hdu3829 Cat VS Dog(最大独立点集)

时间:2023-03-09 00:11:59
Hdu3829 Cat VS Dog(最大独立点集)

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;
}