forked from bicsi/code_snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbipartite_match.cpp
More file actions
51 lines (42 loc) · 926 Bytes
/
Copy pathbipartite_match.cpp
File metadata and controls
51 lines (42 loc) · 926 Bytes
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
struct BipartiteMatcher {
vector<vector<int>> G;
vector<int> L, R, Viz;
BipartiteMatcher(int n, int m) :
G(n), L(n, -1), R(m, -1), Viz(n) {}
void AddEdge(int a, int b) {
G[a].push_back(b);
}
bool Match(int node) {
if (Viz[node])
return false;
Viz[node] = true;
for (auto vec : G[node]) {
if (R[vec] == -1) {
L[node] = vec;
R[vec] = node;
return true;
}
}
for (auto vec : G[node]) {
if (Match(R[vec])) {
L[node] = vec;
R[vec] = node;
return true;
}
}
return false;
}
int Solve() {
int ok = true;
while (ok--) {
fill(Viz.begin(), Viz.end(), 0);
for (int i = 0; i < (int)L.size(); ++i)
if (L[i] == -1)
ok |= Match(i);
}
int ret = 0;
for (int i = 0; i < L.size(); ++i)
ret += (L[i] != -1);
return ret;
}
};