二分圖匹配匈牙利算法
阿新 • • 發佈:2017-05-26
-s ont span fin () turn eof highlight eight
給定一個二分圖,結點個數分別為n,m,邊數為e,求二分圖最大匹配數
輸入樣例#1:1 1 1 1 1輸出樣例#1:
1
#include<bits/stdc++.h> #define maxn 2999 using namespace std; int couple[maxn]; int book[maxn]; int Map[maxn][maxn]; int n,m,e; int ans=0; bool find(int x) { int i,j; for(j=1;j<=m;j++)//掃描所有的妹子 { if(Map[x][j]&&!book[j]) { book[j]=1; if(couple[j]==0||find(couple[j])) {//沒有歸屬,或者歸屬可以被搶走 couple[j]=x; return 1; } } } return 0; } int main() { cin>>n>>m; cin>>e; int from,to; for(int i=1;i<=e;i++) { cin>>from>>to; if(to>m) continue; Map[from][to]=1; } for(int i=1;i<=n;i++) { memset(book,0,sizeof(book)); if(find(i)) ans++ } cout<<ans<<endl; return 0; }
二分圖匹配匈牙利算法