이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
vector<ll> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W){
vector<pii> g[n + 1];
ll tot = 0;
for (int i = 0; i < n - 1; i++){
int u = U[i] + 1, v = V[i] + 1, w = W[i];
g[u].pb({v, w});
g[v].pb({u, w});
tot += w;
}
vector<vector<ll>> dp(n + 1, vector<ll>(n));
function<void(int, int)> dfs = [&](int v, int pr){
for (auto [i, w]: g[v]){
if (i == pr) continue;
dfs(i, v);
}
for (int k = 1; k < n; k++){
dp[v][k] = 0;
vector<ll> all;
for (auto [i, w]: g[v]){
if (i == pr) continue;
dp[v][k] += dp[i][k];
all.pb(dp[i][k - 1] - dp[i][k] + w);
}
sort(all.begin(), all.end(), greater<ll>());
for (int j = 0; j < min((int) all.size(), k); j++){
if (all[j] <= 0) break;
dp[v][k] += all[j];
}
}
for (int k = 1; k < n; k++){
dp[v][k] = max(dp[v][k], dp[v][k - 1]);
}
};
dfs(1, 0);
vector<ll> out;
for (int i = 0; i < n; i++){
out.pb(tot - dp[1][i]);
}
return out;
}
# | 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... |