代码改变世界

杭电 3829 多校联合赛 二分图

2011-11-17 06:44  javaspring  阅读(226)  评论(0编辑  收藏  举报

    这道题想了很久,最后明白了是一个二分图的问题,,就是A喜欢的若是B讨厌的,则AB之间连一条线,BA之间连一条线,,之后就是一个二分图最大独立集的问题,,用总人数-最大匹配数/2就可以了。。。。。。题目:

Cat VS Dog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 1240    Accepted Submission(s): 422


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.
 

ac代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
struct people{
  char ch1,ch2;
  int x,y;
}aa[505];
vector<int> ss[505];
int visted[505],flag[505],panduan[505];
bool dfs(int x){
    for(int i=0;i<ss[x].size();++i){
        if(!visted[ss[x][i]]){
          visted[ss[x][i]]=1;
          if(!flag[ss[x][i]]||dfs(flag[ss[x][i]]))
          {flag[ss[x][i]]=x;/*panduan[ss[x][i]]=1;panduan[x]=1;*/return true;}
        }
    }
    return false;
}
int main(){
    //freopen("1.txt","r",stdin);
  int n,m,p;
  while(scanf("%d%d%d",&n,&m,&p)!=EOF){
      memset(ss,0,sizeof(ss));
    for(int i=1;i<=p;++i){
        cin>>aa[i].ch1>>aa[i].x>>aa[i].ch2>>aa[i].y;
    }
    for(int i=1;i<=p;++i){
        for(int j=1;j<=p;++j){
            if(aa[i].ch1==aa[j].ch2&&aa[i].x==aa[j].y)
               { ss[i].push_back(j);ss[j].push_back(i);}
        }
    }
    memset(flag,0,sizeof(flag));
    memset(panduan,0,sizeof(panduan));
    int num=0;
    for(int i=1;i<=p;++i){
      memset(visted,0,sizeof(visted));
      //if(!panduan[i])
      if(dfs(i))
      {/*printf("i===%d\n",i);printf("size===%d\n",ss[i].size());*/ num++;}
    }
    printf("%d\n",p-num/2);
  }
  return 0;
}