Poj--1274(二分图最大匹配)

本文通过一道经典的二分图最大匹配问题启蒙题,详细介绍了实现匈牙利算法的具体步骤,并提供了完整的C++代码实现。文章推荐了一篇生动形象的博客作为辅助理解资料。

2014-11-02 17:34:20

思路:二分图最大匹配启蒙题~这里要特别推荐一篇神级博客:http://blog.youkuaiyun.com/dark_scope/article/details/8880547,讲的很有趣,生动形象,不拘于论文格式,图文并茂!让人记忆深刻。

 1 /*************************************************************************
 2     > File Name: 1274.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sun 02 Nov 2014 05:15:34 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <stack>
16 #include <queue>
17 #include <iostream>
18 #include <algorithm>
19 using namespace std;
20 #define lp (p << 1)
21 #define rp (p << 1|1)
22 #define getmid(l,r) (l + (r - l) / 2)
23 #define MP(a,b) make_pair(a,b)
24 typedef long long ll;
25 const int INF = 1 << 30;
26 const int maxn = 210;
27 
28 int N,M;
29 int first[maxn],next[maxn * maxn],ver[maxn * maxn],ecnt;
30 int used[maxn],mat[maxn];
31 int g[maxn][maxn];
32 
33 void Init(){
34     memset(first,-1,sizeof(first));
35     memset(mat,0,sizeof(mat));
36     memset(g,0,sizeof(g));
37     ecnt = 0;
38 }
39 
40 void Add_edge(int u,int v){
41     next[++ecnt] = first[u];
42     ver[ecnt] = v;
43     first[u] = ecnt;
44 }
45 
46 bool find(int p){
47     for(int i = first[p]; i != -1; i = next[i]){
48         int v = ver[i];
49         if(used[v] == 0){
50             used[v] = 1;
51             if(mat[v] == 0 || find(mat[v])){
52                 mat[v] = p;
53                 return true;
54             }
55         }
56     }
57     return false;
58 }            
59 
60 int Hungary(){
61     int res = 0;
62     for(int i = 1; i <= N; ++i){
63         memset(used,0,sizeof(used));
64         if(find(i)) ++res;
65     }
66     return res;
67 }
68 
69 int main(){
70     int a,b;
71     while(scanf("%d%d",&N,&M) != EOF){
72         Init();
73         for(int i = 1;i <= N; ++i){
74             scanf("%d",&a);
75             while(a--){
76                 scanf("%d",&b);
77                 Add_edge(i,b);
78             }
79         }
80         printf("%d\n",Hungary());
81     }
82     return 0;
83 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/4069627.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值