From c968af6487755c428e73ec0ab904f1f0a620a80a Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 28 Oct 2022 21:18:23 +0800 Subject: [PATCH] =?UTF-8?q?P3386=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E4=BA=8C=E5=88=86=E5=9B=BE=E6=9C=80=E5=A4=A7=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/91953452 --- Luogu/P3386/P3386.cpp | 93 ++++++++++++++++++++++++++++++------ Luogu/P3386/data/P3386_5.in | 3 ++ Luogu/P3386/data/P3386_5.out | 3 ++ 3 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 Luogu/P3386/data/P3386_5.in create mode 100644 Luogu/P3386/data/P3386_5.out diff --git a/Luogu/P3386/P3386.cpp b/Luogu/P3386/P3386.cpp index f815c95d..7c8fe57d 100644 --- a/Luogu/P3386/P3386.cpp +++ b/Luogu/P3386/P3386.cpp @@ -1,45 +1,108 @@ #include -#include -#include +#include +#include using std::cin; using std::cout; const char endl = '\n'; -const int N = 505; +const int N = 505, + M = 5e4 + 5; +const int INF = 0x3f3f3f3f; -int n, m, e, tag[N], match[N], ans; -std::array, N> g; +int n, m, e, s, t, flow, ans; +int idx, head[N << 1], ver[M << 2], edge[M << 2], next[M << 2]; +int d[N << 1], cur[N << 1]; -bool dfs(int u, int t) { - if (tag[u] == t) return false; +void add(int u, int v, int w) { + next[idx] = head[u]; + ver[idx] = v; + edge[idx] = w; + head[u] = idx++; +} - tag[u] = t; +bool bfs() { + std::fill_n(d, N << 1, 0); - for (int v : g[u]) { - if (!match[v] || dfs(match[v], t)) { - match[v] = u; - return true; + std::queue q; + + q.emplace(s); + d[s] = 1; + cur[s] = head[s]; + + 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]; + + if (w && !d[v]) { + d[v] = d[u] + 1; + cur[v] = head[v]; + if (v == t) return true; + q.emplace(v); + } } } return false; } +int dinic(int u, int limit) { + if (u == t) return limit; + + int flow = 0; + for (int i = cur[u]; ~i && flow < limit; i = next[i]) { + cur[u] = i; + + int v = ver[i], + w = edge[i]; + + if (w && d[v] == d[u] + 1) { + int k = dinic(v, std::min(limit - flow, w)); + + if (!k) d[v] = 0; + + edge[i] -= k; + edge[i ^ 1] += k; + flow += k; + } + } + + return flow; +} + int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); + std::fill_n(head, N << 1, -1); + cin >> n >> m >> e; + 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); + } + for (int i = 1, u, v; i <= e; i++) { cin >> u >> v; - g[u].emplace_back(v); + add(u, n + v, 1); + add(n + v, u, 0); } - for (int i = 1; i <= n; i++) { - if (dfs(i, i)) ans++; + while (bfs()) { + while (flow = dinic(s, INF)) ans += flow; } cout << ans << endl; diff --git a/Luogu/P3386/data/P3386_5.in b/Luogu/P3386/data/P3386_5.in new file mode 100644 index 00000000..eed9d751 --- /dev/null +++ b/Luogu/P3386/data/P3386_5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ad66b356a50c33a10299644877519ec97c57dbee3aec6be595e5efe942ffbce2 +size 2459 diff --git a/Luogu/P3386/data/P3386_5.out b/Luogu/P3386/data/P3386_5.out new file mode 100644 index 00000000..7aaffc3f --- /dev/null +++ b/Luogu/P3386/data/P3386_5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:75ed2f445de0b28c6ba9ea484d8eaec6f02d424f229a569437dfedcf4b8c571c +size 5