이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "roads.h"
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<lint, lint>;
const int MAXN = 100005;
lint dp[MAXN];
int par[MAXN], pae[MAXN];
vector<pi> gph[MAXN];
vector<int> dfn;
void dfs(int x, int p = -1){
dfn.push_back(x);
for(auto &[w, y] : gph[x]){
pae[y] = w;
par[y] = x;
gph[y].erase(find(all(gph[y]), pi(w, x)));
dfs(y, x);
}
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,
std::vector<int> V,
std::vector<int> W) {
for(int i = 0; i < N - 1; i++){
gph[U[i]].emplace_back(W[i], V[i]);
gph[V[i]].emplace_back(W[i], U[i]);
}
dfs(0);
vector<lint> ans(N);
reverse(all(dfn));
ans[0] = accumulate(all(W), 0ll);
vector<vector<int>> cnd(N);
vector<multiset<lint>> ds(N);
vector<lint> sum(N);
auto getsum = [&](multiset<lint> &ms, int k){
lint ret = 0;
auto it = ms.begin();
while(k-- && it != ms.end()){
ret += min(*it, 0ll);
it++;
}
return ret;
};
auto getkth = [&](multiset<lint> &ms, int k){
return getsum(ms, k) - getsum(ms, k-1);
};
for(int i = 1; i < N; i++){
dp[i] = -pae[i];
ds[par[i]].insert(dp[i]);
sum[par[i]] += dp[i];
}
for(auto &i : dfn){
for(int j = 0; j <= sz(gph[i]) + 1 && j < N; j++) cnd[j].push_back(i);
}
for(int k = N - 2; k > 0; k--){
ans[k] = ans[k + 1];
for(auto &j : cnd[k]){
if(sz(gph[j]) > k){
ans[k] -= getkth(ds[j], k+1);
sum[j] -= getkth(ds[j], k+1);
}
}
for(auto &j : cnd[k]){
if(j != 0){
ans[k] -= sum[par[j]];
ds[par[j]].erase(ds[par[j]].find(dp[j]));
}
dp[j] = min(-getkth(ds[j], k) - pae[j], 0ll);
if(j != 0){
ds[par[j]].insert(dp[j]);
sum[par[j]] = getsum(ds[par[j]], k);
ans[k] += sum[par[j]];
}
}
}
return ans;
}
# | 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... |