0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-09 15:58:47 +00:00

P3386 【模板】二分图最大匹配

https://www.luogu.com.cn/record/82488181
This commit is contained in:
Baoshuo Ren 2022-08-04 22:18:05 +08:00
parent b5f87613c9
commit fcc16cfeb0
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

48
Luogu/P3386/P3386.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <iostream>
#include <array>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 505;
int n, m, e, tag[N], match[N], ans;
std::array<std::vector<int>, N> g;
bool dfs(int u, int t) {
if (tag[u] == t) return false;
tag[u] = t;
for (int v : g[u]) {
if (!match[v] || dfs(match[v], t)) {
match[v] = u;
return true;
}
}
return false;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> e;
for (int i = 1, u, v; i <= e; i++) {
cin >> u >> v;
g[u].emplace_back(v);
}
for (int i = 1; i <= n; i++) {
if (dfs(i, i)) ans++;
}
cout << ans << endl;
return 0;
}