-
Notifications
You must be signed in to change notification settings - Fork 185
/
mincut.cc
61 lines (55 loc) · 1.17 KB
/
mincut.cc
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
// https://open.kattis.com/problems/mincut
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using qi = queue<int>;
using vi = vector<int>;
const int N = 500;
int n, s, t, l[N], p[N];
ll g[N][N];
vi ans;
bool dfs(int u, ll c) {
if (u == t) {
while (u != s) g[p[u]][u] -= c, g[u][p[u]] += c, u = p[u];
return 1;
}
for (int v = 0; v < n; v++)
if (l[v] == l[u] + 1 && g[u][v]) {
p[v] = u;
if (dfs(v, min(c, g[u][v]))) return 1;
}
return 0;
}
void dfs2(int u) {
l[u] = 1;
ans.push_back(u);
for (int v = 0; v < n; v++)
if (!l[v] && g[u][v]) dfs2(v);
}
int main() {
cin.tie(0), ios::sync_with_stdio(0);
int m, u, v;
ll c;
cin >> n >> m >> s >> t;
while (m--) cin >> u >> v >> c, g[u][v] += c;
while (1) {
fill(l, l + n, -1);
qi q;
q.push(s);
l[s] = 0;
while (!q.empty()) {
u = q.front();
if (u == t) break;
q.pop();
for (int v = 0; v < n; v++)
if (l[v] < 0 && g[u][v]) l[v] = l[u] + 1, q.push(v);
}
if (q.empty()) break;
while (dfs(s, 1e18))
;
}
fill(l, l + n, 0);
dfs2(s);
cout << ans.size() << '\n';
for (int u : ans) cout << u << '\n';
}