Problem summary
Given a bipartite graph, compute the size of the maximum matching.
Solution
I use Hungarian Algorithm to solve this problem.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <iostream> #include <fstream> #include <cstring> using namespace std; const int maxn = 202; int n,m; int v[maxn][maxn],match[maxn],vis[maxn]; bool find_match(int cur) { int t; for(int i=1;i<=v[cur][0];i++) { t=v[cur][i]; if(vis[t]) continue; vis[t]=true; if(match[t]==0 || find_match(match[t])) { match[t]=cur; return true; } } return false; } int main() { fstream fin("stall4.in",ios::in); fstream fout("stall4.out",ios::out); fin>>n>>m; for(int i=1;i<=n;i++) { fin>>v[i][0]; for(int j=1;j<=v[i][0];j++) fin>>v[i][j]; } int ans=0; for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(find_match(i)) ans++; } fout<<ans<<endl; fin.close(); fout.close(); return 0; }
|