Submission #1034782

#TimeUsernameProblemLanguageResultExecution timeMemory
1034782_8_8_Road Closures (APIO21_roads)C++17
24 / 100
2062 ms33340 KiB
#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 dep[maxn]; vector<ll> pref[maxn],pref1[maxn]; 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; void dfs(int v,int pr = -1){ vis[v] = timer; 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) { 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); vector<int> P(N); iota(P.begin(),P.end(),0); sort(P.begin(),P.end(),[&](int x,int y){ return dep[x] < dep[y]; }); for(int i = 0;i < N;i++) { ll ss = 0; ++timer; k = i; for(int j:P){ if(vis[j] == timer || (int)g[j].size() <= k) continue; dfs(j); ss += dp[j][0]; } res.push_back(ss); } return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...