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>
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,
std::vector<int> V,
std::vector<int> W) {
std::vector<long long> res(N);
std::vector<std::vector<std::pair<int, int>>> adj(N);
long long tot = 0;
for (int i = 0; i + 1 < N; i++) {
int u = U[i], v = V[i], w = W[i];
adj[u].emplace_back(w, v);
adj[v].emplace_back(w, u);
tot += w;
}
std::vector<std::array<long long, 2>> dp(N, {-1, -1});
res[0] = tot;
auto dfs = [&](auto self, int u, int p, bool state) -> long long { // state is for Jj
if (dp[u][state] != -1) return dp[u][state];
int child = 0;
long long ans = 1e18;
for (auto [w, v] : adj[u]) {
if (v == p) continue;
child++;
// now we check it
if (state) {
ans = std::min(ans, w + self(self, v, u, 0));
} else {
// we can either delete or not
auto cand = self(self, v, u, 1);
auto cand2 = w + self(self, v, u, 0); // disconnect it
cand = std::min(cand, cand2);
ans = std::min(ans, cand);
}
}
if (child == 0) {
return dp[u][state] = 0;
} else {
return dp[u][state] = ans;
}
};
res[1] = dfs(dfs, 0, -1, 0);
return res;
}
# | 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... |