您的位置:首页 > 其它

hdu 1528+hdu 1962(最小覆盖)

2013-04-04 21:54 330 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1528

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1962

思路:求二分图的最小覆盖,最小覆盖=最大匹配;

建图略麻烦。。。

View Code

#include<iostream>
const int MAXN=30;
using namespace std;
int n;
bool map[MAXN][MAXN];
int lx[MAXN],ly[MAXN];
bool mark[MAXN];

int dfs(int u){
for(int i=1;i<=n;i++){
if(!mark[i]&&map[u][i]){
mark[i]=true;
if(ly[i]==-1||dfs(ly[i])){
ly[i]=u;
lx[u]=i;
return true;
}
}
}
return false;
}

int MaxMatch(){
int res=0;
memset(lx,-1,sizeof(lx));
memset(ly,-1,sizeof(ly));
for(int i=1;i<=n;i++){
if(lx[i]==-1){
memset(mark,false,sizeof(mark));
res+=dfs(i);
}
}
return res;
}

int main(){
int _case;
scanf("%d",&_case);
while(_case--){
scanf("%d",&n);
char str1[MAXN][4],str2[MAXN][4];
memset(map,false,sizeof(map));
for(int i=1;i<=n;i++){
scanf("%s",str1[i]);
}
for(int i=1;i<=n;i++){
scanf("%s",str2[i]);
for(int j=1;j<=n;j++){
if(str2[i][0]==str1[j][0]){
if(str2[i][1]==str1[j][1])continue;
else if(str2[i][1]=='H'){
map[i][j]=true;
}else if(str2[i][1]=='S'&&str1[j][1]!='H'){
map[i][j]=true;
}else if(str2[i][1]=='D'&&str1[j][1]=='C'){
map[i][j]=true;
}
}else if(str2[i][0]>='2'&&str2[i][0]<='9'){
if(str1[j][0]>='2'&&str1[j][0]<='9'&&str2[i][0]>str1[j][0]){
map[i][j]=true;
}
}else if(str2[i][0]=='T'&&str1[j][0]>='2'&&str1[j][0]<='9'){
map[i][j]=true;
}else if(str2[i][0]=='J'&&(str1[j][0]>='2'&&str1[j][0]<='9'||str1[j][0]=='T')){
map[i][j]=true;
}else if(str2[i][0]=='Q'&&str1[j][0]!='K'&&str1[j][0]!='A'){
map[i][j]=true;
}else if(str2[i][0]=='K'&&str1[j][0]!='A'){
map[i][j]=true;
}else if(str2[i][0]=='A'){
map[i][j]=true;
}
}
}
int ans=MaxMatch();
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: