-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHopcroftKarp.cpp
More file actions
105 lines (101 loc) · 2.4 KB
/
Copy pathHopcroftKarp.cpp
File metadata and controls
105 lines (101 loc) · 2.4 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Author: Kevin Li
* Lang: C++
* Description: Hopcroft-Karp algorithm
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define INF 1e9
#define pb push_back
struct hopcroftkarp {
int nl, nr;
int n;
vector<int> *e;
int *match, *dist;
int mcbm;
hopcroftkarp () {}
hopcroftkarp (int _nl, int _nr) : nl(_nl), nr(_nr) {
n = nl + nr;
e = new vector<int>[n+1];
match = new int[n+1];
dist = new int[n+1];
mcbm = 0;
}
void add(int u, int v) {
e[u+1].pb(v+1);
e[v+1].pb(u+1);
}
bool bfs() {
queue<int> q;
for (int i = 1; i <= nl; i++) {
if (match[i] == 0) {
dist[i] = 0;
q.push(i);
} else {
dist[i] = INF;
}
}
dist[0] = INF;
while (!q.empty()) {
int v = q.front();
q.pop();
if (dist[v] < dist[0]) {
for (int i = 0; i < e[v].size(); i++) {
if (dist[match[e[v][i]]] == INF) {
dist[match[e[v][i]]] = dist[v] + 1;
q.push(match[e[v][i]]);
}
}
}
}
if (dist[0] != INF) {
return true;
}
return false;
}
bool dfs(int node) {
if (node != 0) {
for (int i = 0; i < e[node].size(); i++) {
if (dist[match[e[node][i]]] == dist[node] + 1) {
if (dfs(match[e[node][i]])) {
match[e[node][i]] = node;
match[node] = e[node][i];
return true;
}
}
}
dist[node] = INF;
return false;
}
return true;
}
void driver() {
mcbm = 0;
for (int i = 1; i <= n; i++) {
match[i] = 0;
}
while (bfs()) {
for (int i = 1; i <= nl; i++) {
if (match[i] == 0 && dfs(i)) {
mcbm++;
}
}
}
}
void print() {
cout << mcbm << endl;
}
};
int nl, nr, m;
int main() {
cin >> nl >> nr >> m;
hopcroftkarp HK = hopcroftkarp(nl,nr);
for (int i = 0; i < m; i++) {
int u,v; cin >> u >> v;
HK.add(u,v);
}
HK.driver();
HK.print();
}