2022-08-04 14:18:05 +00:00
|
|
|
#include <iostream>
|
2022-10-28 13:18:23 +00:00
|
|
|
#include <algorithm>
|
2023-02-26 00:20:56 +00:00
|
|
|
#include <iterator>
|
2022-10-28 13:18:23 +00:00
|
|
|
#include <queue>
|
2022-08-04 14:18:05 +00:00
|
|
|
|
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
const char endl = '\n';
|
|
|
|
|
2022-10-28 13:18:23 +00:00
|
|
|
const int N = 505,
|
|
|
|
M = 5e4 + 5;
|
|
|
|
const int INF = 0x3f3f3f3f;
|
2022-08-04 14:18:05 +00:00
|
|
|
|
2023-02-26 00:20:56 +00:00
|
|
|
int n, m, e, s, t;
|
2022-10-28 13:18:23 +00:00
|
|
|
int idx, head[N << 1], ver[M << 2], edge[M << 2], next[M << 2];
|
|
|
|
int d[N << 1], cur[N << 1];
|
2022-08-04 14:18:05 +00:00
|
|
|
|
2022-10-28 13:18:23 +00:00
|
|
|
void add(int u, int v, int w) {
|
|
|
|
ver[idx] = v;
|
|
|
|
edge[idx] = w;
|
2023-02-26 00:20:56 +00:00
|
|
|
next[idx] = head[u];
|
2022-10-28 13:18:23 +00:00
|
|
|
head[u] = idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bfs() {
|
2023-02-26 00:20:56 +00:00
|
|
|
std::fill(std::begin(d), std::end(d), 0);
|
2022-10-28 13:18:23 +00:00
|
|
|
|
|
|
|
std::queue<int> q;
|
2022-08-04 14:18:05 +00:00
|
|
|
|
2022-10-28 13:18:23 +00:00
|
|
|
q.emplace(s);
|
|
|
|
d[s] = 1;
|
|
|
|
cur[s] = head[s];
|
2022-08-04 14:18:05 +00:00
|
|
|
|
2022-10-28 13:18:23 +00:00
|
|
|
while (!q.empty()) {
|
|
|
|
int u = q.front();
|
|
|
|
q.pop();
|
|
|
|
|
|
|
|
for (int i = head[u]; ~i; i = next[i]) {
|
|
|
|
int v = ver[i],
|
|
|
|
w = edge[i];
|
|
|
|
|
2023-02-26 00:20:56 +00:00
|
|
|
if (!d[v] && w) {
|
2022-10-28 13:18:23 +00:00
|
|
|
d[v] = d[u] + 1;
|
|
|
|
cur[v] = head[v];
|
|
|
|
if (v == t) return true;
|
|
|
|
q.emplace(v);
|
|
|
|
}
|
2022-08-04 14:18:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-28 13:18:23 +00:00
|
|
|
int dinic(int u, int limit) {
|
|
|
|
if (u == t) return limit;
|
|
|
|
|
|
|
|
int flow = 0;
|
2023-02-26 00:20:56 +00:00
|
|
|
|
2022-10-28 13:18:23 +00:00
|
|
|
for (int i = cur[u]; ~i && flow < limit; i = next[i]) {
|
|
|
|
cur[u] = i;
|
|
|
|
|
|
|
|
int v = ver[i],
|
|
|
|
w = edge[i];
|
|
|
|
|
2023-02-26 00:20:56 +00:00
|
|
|
if (d[v] == d[u] + 1 && w) {
|
2022-10-28 13:18:23 +00:00
|
|
|
int k = dinic(v, std::min(limit - flow, w));
|
|
|
|
|
|
|
|
if (!k) d[v] = 0;
|
|
|
|
|
2023-02-26 00:20:56 +00:00
|
|
|
flow += k;
|
2022-10-28 13:18:23 +00:00
|
|
|
edge[i] -= k;
|
|
|
|
edge[i ^ 1] += k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flow;
|
|
|
|
}
|
|
|
|
|
2022-08-04 14:18:05 +00:00
|
|
|
int main() {
|
|
|
|
std::ios::sync_with_stdio(false);
|
|
|
|
cin.tie(nullptr);
|
|
|
|
|
2023-02-26 00:20:56 +00:00
|
|
|
std::fill(std::begin(head), std::end(head), -1);
|
2022-10-28 13:18:23 +00:00
|
|
|
|
2022-08-04 14:18:05 +00:00
|
|
|
cin >> n >> m >> e;
|
|
|
|
|
2022-10-28 13:18:23 +00:00
|
|
|
s = 0, t = n + m + 1;
|
|
|
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
add(s, i, 1);
|
|
|
|
add(i, s, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i <= m; i++) {
|
|
|
|
add(n + i, t, 1);
|
|
|
|
add(t, n + i, 0);
|
|
|
|
}
|
|
|
|
|
2022-08-04 14:18:05 +00:00
|
|
|
for (int i = 1, u, v; i <= e; i++) {
|
|
|
|
cin >> u >> v;
|
|
|
|
|
2023-02-26 00:20:56 +00:00
|
|
|
add(u, v + n, 1);
|
|
|
|
add(v + n, u, 0);
|
2022-08-04 14:18:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 00:20:56 +00:00
|
|
|
int res = 0;
|
|
|
|
|
2022-10-28 13:18:23 +00:00
|
|
|
while (bfs()) {
|
2023-02-26 00:20:56 +00:00
|
|
|
while (int flow = dinic(s, INF)) res += flow;
|
2022-08-04 14:18:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 00:20:56 +00:00
|
|
|
cout << res << endl;
|
2022-08-04 14:18:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|