pre文章:匈牙利算法-看这篇绝对就够了!_土豆钊的博客-CSDN博客_匈牙利算法
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3;
bool f[N][N], used[N];
int match[N], n, m, e;
bool DFS(int pos) {
for (int i = 1; i <= m; i++) {
if (f[pos][i] && !used[i]) { //之间存在边,且本次遍历中没有使用
used[i] = true;
if (!match[i] || DFS(match[i])) { //对点没有匹配,后者对点对象可以做出退让
match[i] = pos;
return true;
}
}
}
return false;
}
int main() {
cin >> n >> m >> e;
for (int i = 1; i <= e; i++) {
int x, y; cin >> x >> y; f[x][y] = true;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
memset(used, false, sizeof used); //每次都需要重置used,应为下一个点i可以抢之前同类的对象
if (DFS(i)) ans++;
}
cout << ans << endl;
return 0;
}