HDU 1528 二分图最大匹配

  1 /*HDU 1528
  2 
  3 */
  4 #include <iostream>
  5 #include <cmath>
  6 #include <algorithm>
  7 #include <string.h>
  8 #include <stdio.h>
  9 #include <set>
 10 #include <stack>
 11 #include <queue>
 12 #include <vector>
 13 #define maxn 30
 14 using namespace std;
 15 int num1(char c){
 16     if (c>='2' && c<='9') return c-'0';
 17     if (c=='T') return 10;
 18     if (c=='J') return 11;
 19     if (c=='Q') return 12;
 20     if (c=='K') return 13;
 21     if (c=='A') return 14;
 22 }
 23 int num2(char c){
 24     if (c=='H') return 4;
 25     if (c=='S') return 3;
 26     if (c=='D') return 2;
 27     if (c=='C') return 1;
 28 }
 29 int comp(char sx[],char sy[]){
 30     int k1,k2,t1,t2;
 31     k1=num1(sx[0]);
 32     k2=num1(sy[0]);
 33     t1=num2(sx[1]);
 34     t2=num2(sy[1]);
 35     if (k1==k2) {
 36         if (t1>t2) return 1;
 37         if (t1==t2) return 0;
 38         if (t1<t2) return -1;
 39     }
 40     if (k1<k2) return -1;else return 1;
 41 }
 42 int N ,T;
 43 char s1[maxn][5];
 44 char s2[maxn][5];
 45 bool visit[2*maxn];
 46 int match[2*maxn];
 47 vector<int> G[maxn*2];
 48 
 49 bool dfs(int s)//找到从s点出发的可增广路
 50 {
 51     for(int i=0;i<G[s].size();i++){
 52         int v=G[s][i];
 53         if (!visit[v]){
 54             visit[v]=true;
 55             if (match[v]==0 || dfs(match[v])){//说明v对应的项有增广路
 56                 match[v]=s;//修改v的对应项(即互斥点)是s
 57                 return true;
 58             }
 59         }
 60     }
 61     return false;
 62 }
 63 
 64 int hungary(){
 65     int ans=0;
 66     memset(match,0,sizeof(match));//清空匹配
 67     for(int i=1;i<=N*2;i++){
 68         memset(visit,0,sizeof(visit));//注意清空增广路
 69         if (dfs(i)) ans++;
 70     }
 71     return ans/2;
 72 }
 73 
 74 int main(){
 75     scanf("%d",&T);
 76     while(T--){
 77         scanf("%d",&N);
 78         for(int i=1;i<=N;i++){
 79             scanf("%s",s1[i]);
 80         }
 81         for(int i=1;i<=N;i++){
 82             scanf("%s",s2[i]);
 83         }
 84         for(int i=1;i<=2*N;i++){
 85             G[i].clear();
 86         }
 87         for(int i=1;i<=N;i++){
 88             for(int j=1;j<=N;j++){
 89 //                cout<<"re="<<comp(s2[i],s1[j])<<endl;
 90                 if (comp(s2[i],s1[j])==1){
 91 //                    cout<<i<<","<<j<<endl;
 92                     G[i].push_back(j+N);
 93                     G[j+N].push_back(i);
 94                 }
 95             }
 96         }
 97         printf("%d\n",hungary());
 98     }
 99     return 0;
100 }

 

posted @ 2014-04-07 17:45  little_w  阅读(221)  评论(0编辑  收藏  举报