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;
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
int t, n;
vi a, b;
vector<vii> adj;
int k;
int mem[10000][2];
ll dfs(int s, int p=-1, int cp=0){
if(mem[s][cp]!=-1) return mem[s][cp];
vi al;
ll fix = 0;
int ss = k;
if(cp) ss--;
FOA(v,adj[s]){
if(v.F!=p){
fix += dfs(v.F, s, 0) + v.S;
al.push_back(dfs(v.F, s, 1) - v.S - dfs(v.F, s, 0));
}
}
//cout<<k<<" "<<s<<" "<<fix<<" "<<cp<<endl;
sort(al.begin(), al.end());
FOR(i, min((int)al.size(), ss)) fix += al[i];
mem[s][cp] = fix;
return fix;
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,
std::vector<int> V,
std::vector<int> W) {
n = N;
adj.assign(n+1, vii());
FOR(i,N-1){
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
}
a.resize(n);
FOR(i,n){
FOR(j,n) mem[j][0] = mem[j][1] = -1;
k = i;
a[i] = dfs(0);
}
return a;
}
# | 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... |