hdu 2063+hdu 1083(最大匹配数)

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

二分匹配水题,求最大匹配数(即求边数最多的匹配),匈牙利算法实现。。

View Code
 1 #include<iostream>
 2 const int MAXN=507;
 3 using namespace std;
 4 int k,m,n;
 5 int map[MAXN][MAXN];
 6 int cx[MAXN],cy[MAXN];
 7 int mark[MAXN];
 8 
 9 int dfs(int u){
10     for(int v=0;v<n;v++){
11         if(map[u][v]&&!mark[v]){
12             mark[v]=1;
13             if(cy[v]==-1||dfs(cy[v])){
14                 cx[u]=v;
15                 cy[v]=u;
16                 return 1;
17             }
18         }
19     }
20     return 0;
21 }
22 
23 int MaxMatch(){
24     int res=0;
25     memset(cx,-1,sizeof(cx));
26     memset(cy,-1,sizeof(cy));
27     for(int i=0;i<=m;i++){
28         if(cx[i]==-1){
29             memset(mark,0,sizeof(mark));
30             res+=dfs(i);
31         }
32     }
33     return res;
34 }
35 
36 
37 int main(){
38     while(~scanf("%d",&k)&&k){
39         scanf("%d%d",&m,&n);
40         int x,y;
41         memset(map,0,sizeof(map));
42         for(int i=0;i<k;i++){
43             scanf("%d%d",&x,&y);
44             map[x-1][y-1]=1;//这里只需单向就行了,因为dfs是就是从X集合的未盖点出发的
45         }
46         int ans=MaxMatch();
47         printf("%d\n",ans);
48     }
49     return 0;
50 }
51 
52 
53 
54     

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

View Code
 1 #include<iostream>
 2 const int MAXN=307;
 3 using namespace std;
 4 int cx[MAXN],cy[MAXN];
 5 int map[MAXN][MAXN];
 6 int mark[MAXN];
 7 int n,m;
 8 
 9 int dfs(int u){
10     for(int v=1;v<=m;v++){
11         if(map[u][v]&&!mark[v]){
12             mark[v]=1;
13             if(cy[v]==-1||dfs(cy[v])){
14                 cy[v]=u;
15                 cx[u]=v;
16                 return 1;
17             }
18         }
19     }
20     return 0;
21 }
22 
23 int MaxMatch(){
24     int res=0;
25     memset(cx,-1,sizeof(cx));
26     memset(cy,-1,sizeof(cy));
27     for(int i=1;i<=n;i++){
28         if(cx[i]==-1){
29             memset(mark,0,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%d",&n,&m);
41         memset(map,0,sizeof(map));
42         for(int i=1;i<=n;i++){
43             int t;
44             scanf("%d",&t);
45             while(t--){
46                 int x;
47                 scanf("%d",&x);
48                 map[i][x]=1;
49             }
50         }
51         int ans=MaxMatch();
52         if(ans==n){
53             printf("YES\n");
54         }else 
55             printf("NO\n");
56     }
57     return 0;
58 }
59 
60                 
61             

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值