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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
int N;
vector<pii> adj[MAXN+10];
int par[MAXN+10], val[MAXN+10];
ll ans[MAXN+10];
int K;
ll dp1[MAXN+10], dp2[MAXN+10];
void dfs(int now, int bef)
{
for(auto nxt : adj[now])
{
if(nxt.first==bef) continue;
dfs(nxt.first, now);
par[nxt.first]=now;
val[nxt.first]=nxt.second;
}
}
void dfs2(int now, int bef)
{
vector<ll> V;
ll p=0;
for(auto nxt : adj[now])
{
if(nxt.first==bef) continue;
dfs2(nxt.first, now);
V.push_back(dp1[nxt.first]-dp2[nxt.first]);
p+=dp2[nxt.first];
}
sort(V.begin(), V.end());
for(int i=0; i<K-1 && i<V.size(); i++)
{
if(V[i]<0) p+=V[i];
}
dp1[now]=p;
if(K-1<V.size() && V[K-1]<0) p+=V[K-1];
dp2[now]=p+val[now];
}
vector<ll> minimum_closure_costs(int _N, vector<int> _U, vector<int> _V, vector<int> _W)
{
N=_N;
for(int i=0; i<N-1; i++)
{
int u=_U[i], v=_V[i], w=_W[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs(1, 1);
for(K=0; K<N; K++)
{
dfs2(1, 1);
ans[K]=dp2[1];
}
return vector<ll>(ans, ans+N);
}
Compilation message (stderr)
roads.cpp: In function 'void dfs2(int, int)':
roads.cpp:42:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i=0; i<K-1 && i<V.size(); i++)
| ~^~~~~~~~~
roads.cpp:47:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if(K-1<V.size() && V[K-1]<0) p+=V[K-1];
| ~~~^~~~~~~~~
# | 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... |