#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define el cout << '\n'
const ll INF = 1e18;
struct Edge {
int u, v;
ll cap, flow = 0;
Edge(int u, int v, ll cap) : u(u), v(v), cap(cap) {}
};
struct Dinic {
int n, s, t;
vector<Edge> edges;
vector<vector<int>> adj;
vector<int> level, ptr;
Dinic(int n, int s, int t) : n(n), s(s), t(t) {
adj.resize(n + 1);
level.resize(n + 1);
ptr.resize(n + 1);
}
void add_edge(int u, int v, ll cap) {
adj[u].push_back(edges.size());
edges.emplace_back(u, v, cap);
adj[v].push_back(edges.size());
edges.emplace_back(v, u, 0); // Cạnh ngược capacity = 0
}
bool bfs() {
fill(level.begin(), level.end(), -1);
level[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int id : adj[u]) {
if (edges[id].cap - edges[id].flow > 0 && level[edges[id].v] == -1) {
level[edges[id].v] = level[u] + 1;
q.push(edges[id].v);
}
}
}
return level[t] != -1;
}
ll dfs(int u, ll pushed) {
if (pushed == 0) return 0;
if (u == t) return pushed;
for (int &cid = ptr[u]; cid < adj[u].size(); ++cid) {
int id = adj[u][cid];
int v = edges[id].v;
if (level[v] != level[u] + 1 || edges[id].cap - edges[id].flow == 0) continue;
ll tr = dfs(v, min(pushed, edges[id].cap - edges[id].flow));
if (tr == 0) continue;
edges[id].flow += tr;
edges[id ^ 1].flow -= tr; // Cập nhật cạnh ngược
return tr;
}
return 0;
}
ll max_flow() {
ll flow = 0;
while (bfs()) {
fill(ptr.begin(), ptr.end(), 0);
while (ll pushed = dfs(s, INF)) {
flow += pushed;
}
}
return flow;
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
// Ví dụ sử dụng
int n, m, s, t;
// cin >> n >> m >> s >> t;
// Lưu ý: Khai báo n nodes, source s, sink t
Dinic dinic(n, s, t);
// for(int i=0; i<m; i++) {
// int u, v; ll c; cin >> u >> v >> c;
// dinic.add_edge(u, v, c);
// }
// cout << dinic.max_flow() << el;
return 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aW50PiBxOwogICAgICAgIHEucHVzaChzKTsKICAgICAgICB3aGlsZSAoIXEuZW1wdHkoKSkgewogICAgICAgICAgICBpbnQgdSA9IHEuZnJvbnQoKTsKICAgICAgICAgICAgcS5wb3AoKTsKICAgICAgICAgICAgZm9yIChpbnQgaWQgOiBhZGpbdV0pIHsKICAgICAgICAgICAgICAgIGlmIChlZGdlc1tpZF0uY2FwIC0gZWRnZXNbaWRdLmZsb3cgPiAwICYmIGxldmVsW2VkZ2VzW2lkXS52XSA9PSAtMSkgewogICAgICAgICAgICAgICAgICAgIGxldmVsW2VkZ2VzW2lkXS52XSA9IGxldmVsW3VdICsgMTsKICAgICAgICAgICAgICAgICAgICBxLnB1c2goZWRnZXNbaWRdLnYpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBsZXZlbFt0XSAhPSAtMTsKICAgIH0KCiAgICBsbCBkZnMoaW50IHUsIGxsIHB1c2hlZCkgewogICAgICAgIGlmIChwdXNoZWQgPT0gMCkgcmV0dXJuIDA7CiAgICAgICAgaWYgKHUgPT0gdCkgcmV0dXJuIHB1c2hlZDsKICAgICAgICBmb3IgKGludCAmY2lkID0gcHRyW3VdOyBjaWQgPCBhZGpbdV0uc2l6ZSgpOyArK2NpZCkgewogICAgICAgICAgICBpbnQgaWQgPSBhZGpbdV1bY2lkXTsKICAgICAgICAgICAgaW50IHYgPSBlZGdlc1tpZF0udjsKICAgICAgICAgICAgaWYgKGxldmVsW3ZdICE9IGxldmVsW3VdICsgMSB8fCBlZGdlc1tpZF0uY2FwIC0gZWRnZXNbaWRdLmZsb3cgPT0gMCkgY29udGludWU7CiAgICAgICAgICAgIAogICAgICAgICAgICBsbCB0ciA9IGRmcyh2LCBtaW4ocHVzaGVkLCBlZGdlc1tpZF0uY2FwIC0gZWRnZXNbaWRdLmZsb3cpKTsKICAgICAgICAgICAgaWYgKHRyID09IDApIGNvbnRpbnVlOwogICAgICAgICAgICAKICAgICAgICAgICAgZWRnZXNbaWRdLmZsb3cgKz0gdHI7CiAgICAgICAgICAgIGVkZ2VzW2lkIF4gMV0uZmxvdyAtPSB0cjsgLy8gQ+G6rXAgbmjhuq10IGPhuqFuaCBuZ8aw4bujYwogICAgICAgICAgICByZXR1cm4gdHI7CiAgICAgICAgfQogICAgICAgIHJldHVybiAwOwogICAgfQoKICAgIGxsIG1heF9mbG93KCkgewogICAgICAgIGxsIGZsb3cgPSAwOwogICAgICAgIHdoaWxlIChiZnMoKSkgewogICAgICAgICAgICBmaWxsKHB0ci5iZWdpbigpLCBwdHIuZW5kKCksIDApOwogICAgICAgICAgICB3aGlsZSAobGwgcHVzaGVkID0gZGZzKHMsIElORikpIHsKICAgICAgICAgICAgICAgIGZsb3cgKz0gcHVzaGVkOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBmbG93OwogICAgfQp9OwoKaW50IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOwogICAgCiAgICAvLyBWw60gZOG7pSBz4butIGThu6VuZwogICAgaW50IG4sIG0sIHMsIHQ7CiAgICAvLyBjaW4gPj4gbiA+PiBtID4+IHMgPj4gdDsKICAgIAogICAgLy8gTMawdSDDvTogS2hhaSBiw6FvIG4gbm9kZXMsIHNvdXJjZSBzLCBzaW5rIHQKICAgIERpbmljIGRpbmljKG4sIHMsIHQpOyAKICAgIAogICAgLy8gZm9yKGludCBpPTA7IGk8bTsgaSsrKSB7CiAgICAvLyAgICAgaW50IHUsIHY7IGxsIGM7IGNpbiA+PiB1ID4+IHYgPj4gYzsKICAgIC8vICAgICBkaW5pYy5hZGRfZWRnZSh1LCB2LCBjKTsKICAgIC8vIH0KICAgIAogICAgLy8gY291dCA8PCBkaW5pYy5tYXhfZmxvdygpIDw8IGVsOwogICAgCiAgICByZXR1cm4gMDsKfQ==