-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hopcroft Karp.cpp
56 lines (56 loc) · 1.53 KB
/
Hopcroft Karp.cpp
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
///Hopcroft karp in O(E * sqrt(V))
//Bidirectional edge, if directed then from left to right
// 1 based indexing
#define MAX 500100
namespace hc{
vector<int> adj[MAX];
int match[MAX], dist[MAX], n, m, p;
void init(int nodes){
n = nodes;
for(int i = 0; i <= nodes; i++) adj[i].clear();
}
void add_edge(int u, int v){
adj[u].pb(v);
}
bool bfs() {
queue<int> Q;
for(int i = 1; i <= n; i++){
if(!match[i]) dist[i] = 0, Q.push(i);
else dist[i] = inf;
}
dist[0] = inf;
while(!Q.empty()) {
int u = Q.front(); Q.pop();
if(u == 0) continue;
for(int i = 0; i < adj[u].size(); i++){
int v = adj[u][i];
if(dist[match[v]] == inf) {
dist[match[v]] = dist[u] + 1,
Q.push(match[v]);
}
}
}
return dist[0] != inf;
}
bool dfs(int u) {
if(!u) return 1;
for(int i = 0; i < adj[u].size(); i++){
int v = adj[u][i];
if(dist[match[v]] == dist[u] + 1) {
if(dfs(match[v])) {
match[u] = v, match[v] = u;
return 1;
}
}
}
dist[u] = inf; return 0;
}
int hopcroft_karp(){
int res = 0;
CLR(match);
while(bfs())
for(int i = 1; i <= n; i++)
if(!match[i] && dfs(i)) res += 1;
return res;
}
}