您的位置:首页 > 其它

HDU 1528 二分图最大匹配

2014-04-07 17:45 162 查看
/*HDU 1528

*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#define maxn 30
using namespace std;
int num1(char c){
if (c>='2' && c<='9') return c-'0';
if (c=='T') return 10;
if (c=='J') return 11;
if (c=='Q') return 12;
if (c=='K') return 13;
if (c=='A') return 14;
}
int num2(char c){
if (c=='H') return 4;
if (c=='S') return 3;
if (c=='D') return 2;
if (c=='C') return 1;
}
int comp(char sx[],char sy[]){
int k1,k2,t1,t2;
k1=num1(sx[0]);
k2=num1(sy[0]);
t1=num2(sx[1]);
t2=num2(sy[1]);
if (k1==k2) {
if (t1>t2) return 1;
if (t1==t2) return 0;
if (t1<t2) return -1;
}
if (k1<k2) return -1;else return 1;
}
int N ,T;
char s1[maxn][5];
char s2[maxn][5];
bool visit[2*maxn];
int match[2*maxn];
vector<int> G[maxn*2];

bool dfs(int s)//找到从s点出发的可增广路
{
for(int i=0;i<G[s].size();i++){
int v=G[s][i];
if (!visit[v]){
visit[v]=true;
if (match[v]==0 || dfs(match[v])){//说明v对应的项有增广路
match[v]=s;//修改v的对应项(即互斥点)是s
return true;
}
}
}
return false;
}

int hungary(){
int ans=0;
memset(match,0,sizeof(match));//清空匹配
for(int i=1;i<=N*2;i++){
memset(visit,0,sizeof(visit));//注意清空增广路
if (dfs(i)) ans++;
}
return ans/2;
}

int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(int i=1;i<=N;i++){
scanf("%s",s1[i]);
}
for(int i=1;i<=N;i++){
scanf("%s",s2[i]);
}
for(int i=1;i<=2*N;i++){
G[i].clear();
}
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
//                cout<<"re="<<comp(s2[i],s1[j])<<endl;
if (comp(s2[i],s1[j])==1){
//                    cout<<i<<","<<j<<endl;
G[i].push_back(j+N);
G[j+N].push_back(i);
}
}
}
printf("%d\n",hungary());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: