-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartiteMatching.cpp
More file actions
33 lines (33 loc) · 849 Bytes
/
BipartiteMatching.cpp
File metadata and controls
33 lines (33 loc) · 849 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
int V;
vector<int> g[101010];
int match[101010];
bool used[101010];
void add_edge(int v, int u) {
g[v].push_back(u);
g[u].push_back(v);
}
bool dfs(int v) {
used[v] = true;
for (auto u : g[v]) {
int w = match[u];
if (w < 0 || (!used[w] && dfs(w))) {
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int BipartiteMatching() {
int res = 0;
memset(match, -1, sizeof match);
for (int v = 0; v < V; v ++) {
if (match[v] < 0) {
memset(used, 0, sizeof used);
if (dfs(v)) {
res ++;
}
}
}
return res;
}