-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHopcroftKarp.cpp
More file actions
59 lines (59 loc) · 1.15 KB
/
HopcroftKarp.cpp
File metadata and controls
59 lines (59 loc) · 1.15 KB
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
53
54
55
56
57
58
59
typedef struct HopcroftKarp {
int n, m;
const int INF = 987654321;
vector<int> A, B, dist;
vector<bool> used;
vector<vector<int>> adj;
HopcroftKarp(int _n, int _m) : n(_n), m(_m), adj(_n, vector<int>()) {
}
void add_edge(int st, int en) {
adj[st].push_back(en);
}
void bfs() {
queue<int> Q;
for (int i = 0; i < n; i++) {
if (!used[i]) {
Q.push(i);
dist[i] = 0;
}
else dist[i] = INF;
}
while (!Q.empty()) {
int here = Q.front(); Q.pop();
for (int th : adj[here]) {
if (B[th] != -1 && dist[B[th]] == INF) {
dist[B[th]] = dist[here] + 1;
Q.push(B[th]);
}
}
}
}
bool dfs(int x) {
for (int th : adj[x]) {
if (B[th] == -1 || dist[x] + 1 == dist[B[th]] && dfs(B[th])) {
used[x] = true;
A[x] = th;
B[th] = x;
return true;
}
}
return false;
}
int find_matching() {
A.assign(n, -1); B.assign(m, -1);
used.assign(n, false); dist.assign(n, INF);
int match = 0;
while (true) {
int flow = 0;
bfs();
for (int i = 0; i < n; i++)
if (!used[i] && dfs(i)) flow++;
if (!flow) break;
match += flow;
}
return match;
}
vector<int> match_result() {
return A;
}
}BM;