hdu 1528+hdu 1962(最小覆盖)

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

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

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

建图略麻烦。。。

View Code
 1 #include<iostream>
 2 const int MAXN=30;
 3 using namespace std;
 4 int n;
 5 bool map[MAXN][MAXN];
 6 int lx[MAXN],ly[MAXN];
 7 bool mark[MAXN];
 8 
 9 int dfs(int u){
10     for(int i=1;i<=n;i++){
11         if(!mark[i]&&map[u][i]){
12             mark[i]=true;
13             if(ly[i]==-1||dfs(ly[i])){
14                 ly[i]=u;
15                 lx[u]=i;
16                 return true;
17             }
18         }
19     }
20     return false;
21 }
22 
23 int MaxMatch(){
24     int res=0;
25     memset(lx,-1,sizeof(lx));
26     memset(ly,-1,sizeof(ly));
27     for(int i=1;i<=n;i++){
28         if(lx[i]==-1){
29             memset(mark,false,sizeof(mark));
30             res+=dfs(i);
31         }
32     }
33     return res;
34 }
35 
36 int main(){
37     int _case;
38     scanf("%d",&_case);
39     while(_case--){
40         scanf("%d",&n);
41         char str1[MAXN][4],str2[MAXN][4];
42         memset(map,false,sizeof(map));
43         for(int i=1;i<=n;i++){
44             scanf("%s",str1[i]);
45         }
46         for(int i=1;i<=n;i++){
47             scanf("%s",str2[i]);
48             for(int j=1;j<=n;j++){
49                 if(str2[i][0]==str1[j][0]){
50                     if(str2[i][1]==str1[j][1])continue;
51                     else if(str2[i][1]=='H'){
52                         map[i][j]=true;
53                     }else if(str2[i][1]=='S'&&str1[j][1]!='H'){
54                         map[i][j]=true;
55                     }else if(str2[i][1]=='D'&&str1[j][1]=='C'){
56                         map[i][j]=true;
57                     }
58                 }else if(str2[i][0]>='2'&&str2[i][0]<='9'){
59                     if(str1[j][0]>='2'&&str1[j][0]<='9'&&str2[i][0]>str1[j][0]){
60                         map[i][j]=true;
61                     }
62                 }else if(str2[i][0]=='T'&&str1[j][0]>='2'&&str1[j][0]<='9'){
63                     map[i][j]=true;
64                 }else if(str2[i][0]=='J'&&(str1[j][0]>='2'&&str1[j][0]<='9'||str1[j][0]=='T')){
65                     map[i][j]=true;
66                 }else if(str2[i][0]=='Q'&&str1[j][0]!='K'&&str1[j][0]!='A'){
67                     map[i][j]=true;
68                 }else if(str2[i][0]=='K'&&str1[j][0]!='A'){
69                     map[i][j]=true;
70                 }else if(str2[i][0]=='A'){
71                     map[i][j]=true;
72                 }
73             }
74         }
75         int ans=MaxMatch();
76         printf("%d\n",ans);
77     }
78     return 0;
79 }
原文地址:https://www.cnblogs.com/wally/p/2999907.html