-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhopkroft_karp.cpp
More file actions
147 lines (125 loc) · 2.85 KB
/
hopkroft_karp.cpp
File metadata and controls
147 lines (125 loc) · 2.85 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <numeric>
#include <stack>
#include <fstream>
using namespace std;
template<class TVertex>
vector<pair<TVertex, TVertex>> hk(map<TVertex, vector<TVertex>> &G, vector<TVertex> &G1, vector<TVertex> &G2) {
typedef vector<TVertex> Adjacency;
typedef map<TVertex, Adjacency> Graph;
typedef map<TVertex, TVertex> Pairs;
typedef map<TVertex, int> Distances;
typedef pair<TVertex, TVertex> Pair;
struct Three{
TVertex v;
Adjacency::iterator itr;
bool result;
};
const int INF = numeric_limits<int>::max();
const TVertex FAKE_VERTEX = TVertex(0);
Pairs pair;
Distances dist;
vector<Pair> result;
queue<TVertex> Q;
stack<Three> S;
for (Graph::iterator v = G.begin(); v != G.end(); ++v) {
pair[v->first] = FAKE_VERTEX;
}
while (true) {
for (Adjacency::iterator v = G1.begin(); v != G1.end(); ++v) {
if (pair[*v] == FAKE_VERTEX) {
dist[*v] = 0;
Q.push(*v);
}
else {
dist[*v] = INF;
}
}
dist[FAKE_VERTEX] = INF;
while (!Q.empty()) {
int v = Q.front(); Q.pop();
for (Adjacency::iterator u = G[v].begin(); u != G[v].end(); ++u) {
if (dist[pair[*u]] == INF) {
dist[pair[*u]] = dist[v] + 1;
if (pair[*u] != FAKE_VERTEX) {
Q.push(pair[*u]);
}
}
}
}
if (dist[FAKE_VERTEX] == INF) {
break;
}
for (Adjacency::iterator w = G1.begin(); w != G1.end(); ++w) {
if (pair[*w] != FAKE_VERTEX) {
continue;
}
Three el = {*w, G[*w].begin(), false};
S.push(el);
while (!S.empty()) {
el = S.top();
if (el.result) {
int u = *(el.itr - 1);
pair[el.v] = u;
pair[u] = el.v;
S.pop();
if (!S.empty()) {
S.top().result = true;
}
continue;
}
if (el.itr != G[el.v].end()) {
int u = *el.itr;
S.top().itr = ++el.itr;
if (dist[pair[u]] == dist[el.v] + 1) {
dist[u] = 0;
if (pair[u] != FAKE_VERTEX) {
Three nel = {pair[u], G[pair[u]].begin(), false};
S.push(nel);
} else {
S.top().result = true;
}
}
} else {
dist[el.v] = INF;
S.pop();
}
}
}
}
for (Adjacency::iterator v = G1.begin(); v != G1.end(); ++v) {
if (pair[*v] != FAKE_VERTEX) {
result.push_back(Pair(*v, pair[*v]));
}
}
return result;
}
int test_hk() {
int q, p, m;
ifstream fi("input.txt");
ofstream fo("output.txt");
map<int, vector<int>> G;
vector<int> G1, G2;
fi >> q >> p >> m;
for (int i = 0, u, v; i < m; ++i) {
fi >> u >> v;
v *= -1;
G[u].push_back(v);
}
for (int i = 1; i <= p + q; ++i) {
if (i <= q) {
G1.push_back(i);
} else {
G2.push_back(i - p - q - 1);
}
}
vector<pair<int, int>> res = hk(G, G1, G2);
fo << res.size() << endl;
for (vector<pair<int, int>>::iterator p = res.begin(); p != res.end(); ++p) {
fo << abs(p->first) << " " << abs(p->second) << endl;
}
return 0;
}