-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbipartite_matching(slow).hpp
52 lines (47 loc) · 1.51 KB
/
bipartite_matching(slow).hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once
#include <iostream>
#include <vector>
// CUT begin
// Bipartite matching of undirected bipartite graph
// <https://ei1333.github.io/luzhiled/snippets/graph/bipartite-matching.html>
// Comprexity: O(VE)
struct BipartiteMatching {
int V; // # of vertices
std::vector<std::vector<int>> edges; // Adjacency list
std::vector<int> match; // match[i] = (Partner of i'th node) or -1 (No parter)
std::vector<int> used;
int timestamp;
BipartiteMatching(int V = 0) : V(V), edges(V), match(V, -1), used(V, 0), timestamp(0) {}
void add_edge(int u, int v) {
edges[u].push_back(v);
edges[v].push_back(u);
}
bool dfs(int v) {
used[v] = timestamp;
for (auto to : edges[v]) {
if (match[to] < 0 or (used[match[to]] != timestamp and dfs(match[to]))) {
match[v] = to;
match[to] = v;
return true;
}
}
return false;
}
int solve() // Count up newly formed pairs
{
int ret = 0;
for (int v = 0; v < V; v++)
if (match[v] < 0) {
++timestamp;
ret += dfs(v);
}
return ret;
}
friend std::ostream &operator<<(std::ostream &os, const BipartiteMatching &bm) {
os << "{V=" << bm.V << ":";
for (int i = 0; i < bm.V; i++)
if (i < bm.match[i]) { os << "(" << i << "-" << bm.match[i] << "),"; }
os << "}";
return os;
}
};