| Task: | Download Speed |
| Sender: | hundlij1 |
| Submission time: | 2025-10-19 16:08:17 +0300 |
| Language: | C++ (C++17) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.00 s | details |
| #3 | ACCEPTED | 0.00 s | details |
| #4 | ACCEPTED | 0.00 s | details |
| #5 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 0.01 s | details |
| #7 | ACCEPTED | 0.01 s | details |
| #8 | ACCEPTED | 0.00 s | details |
| #9 | ACCEPTED | 0.00 s | details |
| #10 | ACCEPTED | 0.00 s | details |
| #11 | ACCEPTED | 0.00 s | details |
| #12 | ACCEPTED | 0.00 s | details |
Compiler report
input/code.cpp: In function 'll maxflow(ll, ll)':
input/code.cpp:93:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
93 | while (new_flow = bfs(s, t, parent)) {
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~Code
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <climits>
using namespace std;
typedef long long ll;
ll n, maxLog;
vector<vector<ll>> up;
vector<ll> depth;
ll m;
vector<vector<ll>> capacity;
vector<vector<ll>> adj;
void preprocess(vector<ll>& parent) {
depth[1] = 0;
for (ll i = 2; i <= n; i++) {
depth[i] = depth[parent[i]] + 1;
}
for (ll i = 1; i <= n; i++) {
up[i][0] = parent[i];
}
for (ll k = 1; k < maxLog; k++) {
for (ll node = 1; node <= n; node++) {
up[node][k] = up[up[node][k-1]][k-1];
}
}
}
ll ancestor(ll x, ll k) {
for (ll i = 0; i < maxLog; i++) {
if (k & (1 << i)) {
x = up[x][i];
if (x == 0) return 0;
}
}
return x;
}
ll lca(ll a, ll b) {
if (depth[a] < depth[b]) swap(a, b);
a = ancestor(a, depth[a] - depth[b]);
if (a == b) return a;
for (ll k = maxLog - 1; k >= 0; k--) {
if (up[a][k] != up[b][k]) {
a = up[a][k];
b = up[b][k];
}
}
return up[a][0];
}
ll bfs(ll s, ll t, vector<ll>& parent) {
fill(parent.begin(), parent.end(), -1);
parent[s] = -2;
queue<pair<ll, ll>> q;
q.push({s, LLONG_MAX});
while (!q.empty()) {
ll cur = q.front().first;
ll flow = q.front().second;
q.pop();
for (ll next : adj[cur]) {
if (parent[next] == -1 && capacity[cur][next]) {
parent[next] = cur;
ll new_flow = min(flow, capacity[cur][next]);
if (next == t) return new_flow;
q.push({next, new_flow});
}
}
}
return 0;
}
ll maxflow(ll s, ll t) {
ll flow = 0;
vector<ll> parent(n + 1);
ll new_flow;
while (new_flow = bfs(s, t, parent)) {
flow += new_flow;
ll cur = t;
while (cur != s) {
ll prev = parent[cur];
capacity[prev][cur] -= new_flow;
capacity[cur][prev] += new_flow;
cur = prev;
}
}
return flow;
}
int main() {
/*ll q;
cin >> n >> q;
maxLog = ceil(log2(n)) + 1;
up.assign(n + 1, vector<ll>(maxLog, 0));
depth.assign(n + 1, 0);
vector<ll> parent(n + 1, 0);
for(ll i = 2; i <= n; i++){
cin >> parent[i];
}
preprocess(parent);
for(ll i = 0; i < q; i++){
ll a, b;
cin >> a >> b;
cout << lca(a, b) << "\n";
}
return 0;*/
cin >> n >> m;
capacity.assign(n + 1, vector<ll>(n + 1, 0));
adj.assign(n + 1, vector<ll>());
for (ll i = 0; i < m; i++) {
ll a, b, c;
cin >> a >> b >> c;
capacity[a][b] += c;
adj[a].push_back(b);
adj[b].push_back(a);
}
cout << maxflow(1, n) << "\n";
return 0;
}Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 4 3 1 2 5 2 3 3 3 4 6 |
| correct output |
|---|
| 3 |
| user output |
|---|
| 3 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 4 5 1 2 1 1 3 1 2 3 1 2 4 1 ... |
| correct output |
|---|
| 2 |
| user output |
|---|
| 2 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 4 5 1 2 1000000000 1 3 1000000000 2 3 1 2 4 1000000000 ... |
| correct output |
|---|
| 2000000000 |
| user output |
|---|
| 2000000000 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 2 1 2 1 100 |
| correct output |
|---|
| 0 |
| user output |
|---|
| 0 |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 2 1000 1 2 1000000000 1 2 1000000000 1 2 1000000000 1 2 1000000000 ... |
| correct output |
|---|
| 1000000000000 |
| user output |
|---|
| 1000000000000 |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 500 998 1 2 54 1 3 59 1 4 83 2 5 79 ... |
| correct output |
|---|
| 60 |
| user output |
|---|
| 60 |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 500 998 1 2 530873053 1 3 156306296 1 4 478040476 3 5 303609600 ... |
| correct output |
|---|
| 1093765123 |
| user output |
|---|
| 1093765123 |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 2 1 1 2 1 |
| correct output |
|---|
| 1 |
| user output |
|---|
| 1 |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 4 5 1 2 3 2 4 2 1 3 4 3 4 5 ... |
| correct output |
|---|
| 6 |
| user output |
|---|
| 6 |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 4 5 1 2 1 1 3 2 3 2 1 2 4 2 ... |
| correct output |
|---|
| 3 |
| user output |
|---|
| 3 |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 10 999 1 2 1000000000 1 2 1000000000 1 2 1000000000 1 2 1000000000 ... |
| correct output |
|---|
| 111000000000 |
| user output |
|---|
| 111000000000 |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 7 9 1 2 1 1 3 1 1 4 1 2 5 1 ... |
| correct output |
|---|
| 2 |
| user output |
|---|
| 2 |
