Skip to content

Commit 7d58a84

Browse files
authored
adding mvc here until kactl approves PR (#217)
1 parent 9908875 commit 7d58a84

4 files changed

Lines changed: 37 additions & 118 deletions

File tree

library/graphs/hopcroft_karp.hpp

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "../../kactl/content/graph/HopcroftKarp.h"
2+
pair<vi, vi> cover(const vector<vi>& g, vi& r) {
3+
int n = sz(g), t = 0;
4+
vi cl(n), cr(sz(r)), q(n);
5+
for (int u : r)
6+
if (u != -1) cl[u] = 1;
7+
rep(i, 0, n) if (!cl[i]) q[t++] = i;
8+
rep(i, 0, t) for (int v : g[q[i]]) {
9+
cr[v] = 1;
10+
if (r[v] != -1 && cl[r[v]]) cl[q[t++] = r[v]] = 0;
11+
}
12+
return {cl, cr};
13+
}
Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#define PROBLEM \
22
"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_7_A"
33
#include "../template.hpp"
4-
#include "../../../library/graphs/hopcroft_karp.hpp"
4+
#include "../../../library/graphs/min_vertex_cover.hpp"
55
int main() {
66
cin.tie(0)->sync_with_stdio(0);
77
int l, r, m;
@@ -14,34 +14,18 @@ int main() {
1414
adj[u].push_back(v);
1515
edges.emplace_back(u, v);
1616
}
17-
hopcroft_karp res(adj, r);
18-
cout << res.m_sz << '\n';
19-
// asserting correctness of both to_r, and to_l
20-
int size_l = 0;
21-
for (int i = 0; i < l; i++) {
22-
if (res.to_r[i] != -1) {
23-
size_l++;
24-
int node_r = res.to_r[i];
25-
assert(res.to_l[node_r] == i);
26-
}
27-
}
17+
vi ri(r, -1);
18+
int size_matching = hopcroftKarp(adj, ri);
19+
auto [mvc_l, mvc_r] = cover(adj, ri);
2820
int size_r = 0;
29-
for (int i = 0; i < r; i++) {
30-
if (res.to_l[i] != -1) {
31-
size_r++;
32-
int node_l = res.to_l[i];
33-
assert(res.to_r[node_l] == i);
34-
}
35-
}
36-
assert(size_l == res.m_sz);
37-
assert(size_r == res.m_sz);
21+
for (int i = 0; i < r; i++)
22+
if (ri[i] != -1) size_r++;
23+
assert(size_r == size_matching);
3824
// asserting found min vertex cover is correct
39-
int cnt =
40-
accumulate(begin(res.mvc_l), end(res.mvc_l), 0) +
41-
accumulate(begin(res.mvc_r), end(res.mvc_r), 0);
42-
assert(cnt == res.m_sz); // size of min vertex cover
43-
// == size of max matching
44-
for (auto [u, v] : edges)
45-
assert(res.mvc_l[u] || res.mvc_r[v]);
25+
int cnt = accumulate(begin(mvc_l), end(mvc_l), 0) +
26+
accumulate(begin(mvc_r), end(mvc_r), 0);
27+
assert(cnt == size_matching);
28+
for (auto [u, v] : edges) assert(mvc_l[u] || mvc_r[v]);
29+
cout << size_matching << '\n';
4630
return 0;
4731
}
Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#define PROBLEM \
22
"https://judge.yosupo.jp/problem/bipartitematching"
33
#include "../template.hpp"
4-
#include "../../../library/graphs/hopcroft_karp.hpp"
4+
#include "../../../library/graphs/min_vertex_cover.hpp"
55
int main() {
66
cin.tie(0)->sync_with_stdio(0);
77
int l, r, m;
@@ -14,36 +14,21 @@ int main() {
1414
adj[u].push_back(v);
1515
edges.emplace_back(u, v);
1616
}
17-
hopcroft_karp res(adj, r);
18-
cout << res.m_sz << '\n';
19-
// asserting correctness of both to_r, and to_l (as
20-
// well as printing answer)
21-
int size_l = 0;
22-
for (int i = 0; i < l; i++) {
23-
if (res.to_r[i] != -1) {
24-
size_l++;
25-
int node_r = res.to_r[i];
26-
cout << i << " " << node_r << '\n';
27-
assert(res.to_l[node_r] == i);
28-
}
29-
}
17+
vi ri(r, -1);
18+
int m_sz = hopcroftKarp(adj, ri);
19+
auto [mvc_l, mvc_r] = cover(adj, ri);
20+
cout << m_sz << '\n';
3021
int size_r = 0;
3122
for (int i = 0; i < r; i++) {
32-
if (res.to_l[i] != -1) {
23+
if (ri[i] != -1) {
3324
size_r++;
34-
int node_l = res.to_l[i];
35-
assert(res.to_r[node_l] == i);
25+
cout << ri[i] << ' ' << i << '\n';
3626
}
3727
}
38-
assert(size_l == res.m_sz);
39-
assert(size_r == res.m_sz);
40-
// asserting found min vertex cover is correct
41-
int cnt =
42-
accumulate(begin(res.mvc_l), end(res.mvc_l), 0) +
43-
accumulate(begin(res.mvc_r), end(res.mvc_r), 0);
44-
assert(cnt == res.m_sz); // size of min vertex cover
45-
// == size of max matching
46-
for (auto [u, v] : edges)
47-
assert(res.mvc_l[u] || res.mvc_r[v]);
28+
assert(size_r == m_sz);
29+
int cnt = accumulate(begin(mvc_l), end(mvc_l), 0) +
30+
accumulate(begin(mvc_r), end(mvc_r), 0);
31+
assert(cnt == m_sz);
32+
for (auto [u, v] : edges) assert(mvc_l[u] || mvc_r[v]);
4833
return 0;
4934
}

0 commit comments

Comments
 (0)