HDU3829--Cat VS Dog

Cat VS Dog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 2068 Accepted Submission(s): 728


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.

 

将支持猫,反对狗的分为一组,将支持狗,反对猫的分为一组。

求最大独立集合。

  1 #include<cstdio>
  2 #include<cmath>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<map>
  7 #include<string>
  8 using namespace std;
  9 
 10 int link[1001][1001];
 11 int cx[1001];
 12 int cy[1001];
 13 int mk[1001];
 14 struct Node
 15 {
 16     char bbegin[1001];
 17     char eend[1001];
 18 }c[1000],d[1000];
 19 int nx,ny;
 20 int t;
 21 int cnum,dnum;
 22 
 23 void init()
 24 {
 25     memset(link,0,sizeof(link));
 26     memset(cx,0xff,sizeof(cx));
 27     memset(cy,0xff,sizeof(cy));
 28     memset(mk,0,sizeof(mk));
 29 }
 30 
 31 int path(int u)
 32 {
 33     int i;
 34     for(i=1;i<dnum;i++)
 35     {
 36         if(!mk[i]&&link[u][i])
 37         {
 38             mk[i]=1;
 39             if(cy[i]==-1||path(cy[i]))
 40             {
 41                 cx[u]=i;
 42                 cy[i]=u;
 43                 return 1;
 44             }
 45         }
 46     }
 47     return 0;
 48 }
 49 
 50 int maxmatch()
 51 {
 52     int i;
 53     int sum=0;
 54     for(i=1;i<cnum;i++)
 55     {
 56         if(cx[i]==-1)
 57         {
 58             memset(mk,0,sizeof(mk));
 59             sum+=path(i);
 60         }
 61     }
 62     return sum;
 63 }
 64 
 65 int main()
 66 {
 67     while(scanf("%d%d%d",&nx,&ny,&t)!=EOF)
 68     {
 69         init();
 70         char s[200],e[200];
 71         int i,j;
 72         cnum=dnum=1;
 73         for(i=1;i<=t;i++)
 74         {
 75             scanf("%s%s",s,e);
 76             if(s[0]=='C')
 77             {
 78                 strcpy(c[cnum].bbegin,s);
 79                 strcpy(c[cnum++].eend,e);
 80             }
 81             else
 82             {
 83                 strcpy(d[dnum].bbegin,s);
 84                 strcpy(d[dnum++].eend,e);
 85             }
 86         }
 87         for(i=1;i<cnum;i++)
 88         {
 89             for(j=1;j<dnum;j++)
 90             {
 91                 if(strcmp(c[i].bbegin,d[j].eend)==0||strcmp(c[i].eend,d[j].bbegin)==0)
 92                 {
 93                     link[i][j]=1;
 94                 }
 95             }
 96         }
 97         int ans=maxmatch();
 98         printf("%d\n",t-ans);
 99     }
100     return 0;
101 }
View Code

 

posted on 2013-07-27 20:01  张狂不年轻°  阅读(187)  评论(0编辑  收藏  举报