이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 aint(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] ;
ll dp[maxn][2] , k;
void dfs(int v, int p = -1){
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(u == p)continue ;
dfs(u , v) ;
dp[v][0] += dp[u][1] ;
pq.push((-dp[u][1] + dp[u][0] + 1ll*w));
while(sz(pq) > x)pq.pop();
}
dp[v][1] = dp[v][0] ;
if(x==0)return ;
if(sz(pq) < x){
dp[v][1] = inf ;
while(sz(pq)){
dp[v][0] += pq.top() ; pq.pop() ;
}
}else{
while(sz(pq)){
ll z = pq.top() ;pq.pop() ;
if(sz(pq) != 0)dp[v][0] += z ;
dp[v][1] += z;
}
}
}
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<ll> ans ;
rep(i , 0 , n-1){
k = i ;dfs(0);
ans.pb(dp[0][1]);
}
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... |