匈牙利算法详解
//匈牙利算法,邻接表建图
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = ;//max(n,m),n为左图节点数,m为右图节点数
int head[maxn],ver[maxn],Next[maxn];//邻接表,表头是左图节点,链表记录与之相连的右图节点
int match[maxn];//match记录当前匹配下右图与节点相连接的左图匹配点
bool vis[maxn];//判右图节点是否遍历过
int ans;
bool dfs(int x){
for(int i=head[x],y;i;i=Next[i]){
if(!vis[y=ver[i]]){
vis[y]=true;
if(!match[y]||dfs(match[y])){//dfs(match[y]):为match[y]重新分配一个匹配点
match[y]=x;
return true;
}
}
}
return false;
}
int main(){
ans=0;//最大匹配数
for(int i=1;i<=n;i++){//n为左图节点数
memset(vis,false,sizeof(vis));
if(dfs(i)) ans++;
}
return 0;
}