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 <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = (int)1e5 +12;
vector<pair<int,int>> g[maxn];
ll dp[maxn][2];
int k;
int pos[maxn],id[maxn];
int dep[maxn];
vector<ll> pref[maxn],pref1[maxn];
int n;
int t[maxn * 4];
vector<int> P;
void build(int v = 1,int tl = 0,int tr = n - 1){
if(tl == tr) {
t[v] = (int)g[P[tl]].size();
}else {
int tm = (tl + tr) >> 1;
build(v + v,tl,tm);
build(v + v + 1,tm + 1,tr);
t[v] = max(t[v + v],t[v + v + 1]);
}
}
void upd(int pos,int val,int v = 1,int tl = 0,int tr = n - 1){
if(tl == tr) {
t[v] = val;
}else {
int tm = (tl + tr) >> 1;
if(pos <= tm) upd(pos,val,v+v,tl,tm);
else upd(pos,val,v+v+1,tm+1,tr);
t[v] = max(t[v + v],t[v + v + 1]);
}
}
int get(int v = 1,int tl = 0,int tr = n - 1){
if(t[v] <= k) return -1;
if(tl == tr){
// cout << tl << ' ' << t[v] << "x\n";
return P[tl];
}else{
int tm = (tl + tr) >> 1;
if(t[v + v] > k) return get(v+v,tl,tm);
return get(v+v+1,tm+1,tr);
}
}
void prec(int v,int pr = -1,int _w = 0) {
for(auto [to,w]:g[v]) {
if(to == pr) continue;
dep[to] = dep[v] + 1;
prec(to,v,w);
pref[v].push_back(w);
pref1[v].push_back(w);
}
if(pr != -1){
pref1[v].push_back(_w);
}
sort(pref[v].rbegin(),pref[v].rend());
for(int i = 1;i < (int)pref[v].size();i++) {
pref[v][i] += pref[v][i - 1];
}
sort(pref1[v].rbegin(),pref1[v].rend());
for(int i = 1;i < (int)pref1[v].size();i++) {
pref1[v][i] += pref1[v][i - 1];
}
}
int vis[maxn],timer = 0;
vector<pair<int,int>> bf;
void dfs(int v,int pr = -1){
vis[v] = timer;
// cout << id[v] << "A\n";
upd(id[v],0);
bf.push_back({id[v],(int)g[v].size()});
if(g[v].empty() || (int)g[g[v][0].first].size() > k) {
ll S = 0;
vector<ll> can;
for(auto [to,w]:g[v]) {
if(to == pr) continue;
if((int)g[to].size() > k) {
dfs(to,v);
}else {
dp[to][0] = dp[to][1] = 0;
}
S += dp[to][0] + w;
can.push_back(dp[to][1] - (dp[to][0] + w));
}
sort(can.begin(),can.end());
dp[v][0] = dp[v][1] = S;
for(int i = 0;i < (int)can.size() && can[i] < 0;i++){
if(i < k){
dp[v][0] += can[i];
}
if(i < k - 1)
{
dp[v][1] += can[i];
}
}
}else{
if(pr == -1){
if(pref1[v].empty()){
dp[v][0] = dp[v][1] = 0;
return;
}
dp[v][0] = dp[v][1] = pref1[v].back();
if(k - 1 >= (int)pref1[v].size()){
dp[v][0] = 0;
}else{
dp[v][0] -= pref1[v][k - 1];
}
if(k - 2 >= (int)pref1[v].size()){
dp[v][1] = 0;
}else{
dp[v][1] -= pref1[v][k - 2];
}
}else {
if(pref[v].empty()){
dp[v][0] = dp[v][1] = 0;
return;
}
dp[v][0] = dp[v][1] = pref[v].back();
if(k - 1 >= (int)pref[v].size()){
dp[v][0] = 0;
}else{
dp[v][0] -= pref[v][k - 1];
}
if(k - 2 >= (int)pref[v].size()){
dp[v][1] = 0;
}else{
dp[v][1] -= pref[v][k - 2];
}
}
}
}
vector<long long> minimum_closure_costs(int N,vector<int> U,vector<int> V,vector<int> W) {
n = N;
P.resize(n);
for(int i = 0;i < N - 1;i++){
g[U[i]].emplace_back(V[i],W[i]);
g[V[i]].emplace_back(U[i],W[i]);
}
for(int i = 0;i < N;i++) {
sort(g[i].begin(),g[i].end(),[&](pair<int,int> x,pair<int,int> y) {
return ((int)g[x.first].size()) > (int)g[y.first].size();
});
}
vector<ll> res;
prec(0);
iota(P.begin(),P.end(),0);
sort(P.begin(),P.end(),[&](int x,int y){
return dep[x] < dep[y];
});
build();
for(int i = 0;i < N;i++) {
id[P[i]] = i;
}
for(int i = 0;i < N;i++) {
ll ss = 0;
++timer;
k = i;
bf.clear();
while(true){
int v = get();
// cout << v << '\n';
if(v!=-1){
dfs(v);
ss += dp[v][0];
}else break;
}
for(int j:P){
if(vis[j] == timer || (int)g[j].size() <= k) continue;
dfs(j);
ss += dp[j][0];
}
for(auto [x,y]:bf){
upd(x,y);
}
res.push_back(ss);
}
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... |