이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |