-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbipartite_matching.h
50 lines (46 loc) · 1.02 KB
/
bipartite_matching.h
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
class BipartiteMatching{
private:
int n; // 頂点数
vector<vector<int>> graph;
vector<int> match;
vector<int> used;
bool Dfs(int v);
public:
BipartiteMatching(int n);
void AddEdge(int u, int v);
int Run();
int Match(int u);
};
BipartiteMatching::BipartiteMatching(int n):
n(n),
graph(n),
match(n),
used(n)
{}
void BipartiteMatching::AddEdge(int u, int v){
graph[u].push_back(v);
graph[v].push_back(u);
}
bool BipartiteMatching::Dfs(int v){
used[v] = true;
for(int u : graph[v]){
int w = match[u];
if(w == -1 || (!used[w] && Dfs(w))){
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int BipartiteMatching::Run(){
int res = 0;
fill(begin(match), end(match), -1);
for(int v = 0; v < n; ++v){
if(match[v] == -1){
fill(begin(used), end(used), false);
if(Dfs(v)) res++;
}
}
return res;
}