# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
971277 | Pannda | Road Closures (APIO21_roads) | C++17 | 2025 ms | 1048576 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
vector<long long> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W) {
vector<vector<array<int, 2>>> adj(n);
for (int i = 0; i < n - 1; i++) {
auto [u, v, w] = array<int, 3>{U[i], V[i], W[i]};
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
vector<vector<array<long long, 2>>> f(n);
auto dfs = [&](auto self, int u, int p) -> void {
int K = n - 1;
f[u].resize(K + 1);
vector<long long> sum(K + 1, 0);
vector<vector<long long>> delta(K + 1);
for (auto [v, w] : adj[u]) {
if (v == p) continue;
self(self, v, u);
for (int k = 1; k <= K; k++) {
sum[k] += f[v][k][1];
delta[k].push_back(f[v][k][0] + w - f[v][k][1]);
}
}
int num_childs = adj[u].size() - (p != -1);
for (int k = 1; k <= K; k++) {
sort(delta[k].begin(), delta[k].end());
int i = 0;
while (i < delta[k].size() && delta[k][i] < 0) i++;
f[u][k][0] = sum[k] + accumulate(delta[k].begin(), delta[k].begin() + max(i, num_childs - k), 0LL);
f[u][k][1] = sum[k] + accumulate(delta[k].begin(), delta[k].begin() + max(i, num_childs + 1 - k), 0LL);
}
};
dfs(dfs, 0, -1);
vector<long long> res(n);
res[0] = accumulate(W.begin(), W.end(), 0LL);
for (int k = 1; k < n; k++) {
res[k] = f[0][k][0];
}
return res;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |