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 <vector>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cstring>
#include <vector>
#include <string>
#include <climits>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
#include <list>
using namespace std;
#define all(x) x.begin(), x.end()
#define rll(x) x.rbegin(), x.rend()
#define comp(x) x.erase(unique(all(x)), x.end())
#define MOD 1000000007
#define MOD2 998244353
#define debug(x) cout << #x<<" is "<<x<<"\n";
#define X first
#define Y second
#define DEB cout<<"[DEBUG]"
#define PAIR(a,b) "("<<a<<", "<<b<<")"
#define PRINT1(V) DEB<<#V<<endl; for(auto i : V)DEB<<i<<"\n"
#define PRINT2(V) DEB<<#V<<endl; for(auto [a,b] : V)DEB<<PAIR(a,b)<<"\n";
typedef long long ll;
typedef __int128_t lll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
typedef pair<ld,ll> Pd;
ll dp[202020][2];
ll n, k;
vector<P> adj[202020];
void dfs(ll cur, ll prv=-1){
priority_queue<ll> pq;
for(auto [next,w] : adj[cur])if(prv!=next){
dfs(next,cur);
if(dp[next][0] < dp[next][1] + w)pq.push(dp[next][1] + w - dp[next][0]);
dp[cur][0] += dp[next][0], dp[cur][1] += dp[next][0];
}
vector<ll> v;
ll pqsiz = pq.size();
for(int i = 0 ; i < min<ll>(pqsiz, k) ; i++)v.push_back(pq.top()), pq.pop();
for(int i = 0 ; i < min<ll>(v.size(), k) ; i++)dp[cur][0] += v[i];
for(int i = 0 ; i < min<ll>(v.size(), k-1) ; i++)dp[cur][1] += v[i];
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
vector<ll> ret(N);
n=N;
ll S = 0;
for(int i = 0 ; i < n-1 ; i++){
S += W[i];
adj[U[i]].push_back({V[i],W[i]});
adj[V[i]].push_back({U[i],W[i]});
}
for(int i = 0 ; i < n ; i++){
if(i>=2){
ret[i] = 0;
continue;
}
k=i;
memset(dp,0,sizeof(dp));
dfs(0);
ret[i] = S - dp[0][0];
}
return ret;
}
# | 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... |