HDU2768Cat vs. Dog二分图 最大独立集(最大匹配)

来源:互联网 发布:压力校验台淘宝 编辑:程序博客网 时间:2024/06/11 04:47

今天做的二分图唯一一个比较难的 而且看了题解才发现,又理解错题意了。是说 如果出现喜欢的和别人讨厌的相同,则其中一人会不满意。问最多满意多少人

即把喜欢喝讨厌彼此矛盾的建边,个数-最大匹配即为所求

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

21 1 2C1 D1D1 C11 2 4C1 D1C1 D1C1 D2D2 C1
 

Sample Output

13
 

Source

#include <iostream>#include <stdio.h>#include <string.h>#define M 550#define inf 0x3f3f3f3fusing namespace std;const int MAXN=550;int uN,vN;//u,v数目int g[MAXN][MAXN];int linker[MAXN];bool used[MAXN];bool dfs(int u)//从左边开始找增广路径{    int v;    for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改      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;}char like[550][10],dislike[550][10];int main(){ //   freopen("cin.txt","r",stdin);    int T,c,d,v;    while(~scanf("%d",&T))    {        while(T--)        {            scanf("%d%d%d",&c,&d,&v);            vN=uN=v;            for(int i=0;i<v;i++) scanf("%s%s",like[i],dislike[i]);            memset(g,0,sizeof(g));            for(int i=0;i<v;i++)                for(int j=0;j<v;j++)                {                    if(strcmp(like[i],dislike[j])==0||strcmp(like[j],dislike[i])==0)                    g[i][j]=1;                }            printf("%d\n",v-hungary()/2);        }    }    return 0;}


0 0