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 <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second
#define ld long double
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define ll long long
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= (b);i++)
#define per(i , a , b) for(int i=a;i >= (b);i--)
#define deb(x) cout <<#x << " : " << x << "\n" ;
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn = 1e6 , maxq = 32, inf = 1e15+10 , lg = 19 ,sq = 707 ,mod = 998244353 ;
vector <pii> G[maxn] ;
int mark[maxn] ;vector <int> his ;
ll dp[maxn][2] , k;
void dfs(int v , int W = 0){
mark[v] = 1; his.pb(v);
dp[v][0] = dp[v][1] = 0;
int x = max(0ll,sz(G[v])-k) ;
priority_queue <ll> pq ;
for(auto [u,w] : G[v]){
if(mark[u] == 1)continue ;
if(sz(G[u]) > k){
dfs(u , w) ;
}else{
dp[u][0] = w ;
dp[u][1] = 0;
}
if(dp[u][0] <= dp[u][1]){
dp[v][0] += dp[u][0] ;
x=max(x-1 , 0) ;
}else{
dp[v][0] += dp[u][1] ;
pq.push((-dp[u][1] + dp[u][0]));
while(sz(pq) > x)pq.pop();
}
}
dp[v][1] = dp[v][0] ;
dp[v][0] += W ;
if(x==0)return ;
while(sz(pq) > x)pq.pop();
if(sz(pq) < x){
while(sz(pq)){
dp[v][0] += pq.top() ; pq.pop() ;
}
dp[v][1] = inf ;
}else{
bool ok =0 ;
while(sz(pq)){
ll z = pq.top() ;pq.pop() ;
if(ok)dp[v][0] += z ;
ok = 1;
dp[v][1] += z;
}
}
}
vector<ll> ans ;
vector<ll> minimum_closure_costs(int n, vector<int> U,vector<int> V,vector<int> W) {
rep(i ,0 , n-2){
G[V[i]].pb({U[i] , W[i]}) ;
G[U[i]].pb({V[i] , W[i]}) ;
}
vector<pii> deg ;
rep(i , 0 , n-1){
deg.pb({sz(G[i]) ,i});
}
sort(all(deg));
rep(i , 0 , n-1){
k = i ;ll f =0 ;
per(j , n-1 , 0){
if(deg[j].F <= k)break ;
if(mark[deg[j].S] == 1)continue ;
dfs(deg[j].S , 0);
f += dp[deg[j].S][1] ;
}
for(int x : his)mark[x] = 0;
ans.pb(f);
}
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... |