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 <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define pii pair<int, int>
#define ve vector
#define vei vector<int>
#define vell vector<ll>
#define sz(x) ((int)x.size())
#define each(it, x) for (auto &it : x)
#define debug(x) cout << #x << " = " << x << "\n";
#define FU function
#define VO void
vell minimum_closure_costs(int N, vei U, vei V, vei W) {
int n = N;
assert(sz(U) == n - 1);
assert(sz(V) == n - 1);
assert(sz(W) == n - 1);
#ifdef ONPC
debug(n);
each(it, U) cout << it << " "; cout << "\n";
each(it, V) cout << it << " "; cout << "\n";
each(it, W) cout << it << " "; cout << "\n";
#endif
ve<ve<pii>> g(n);
for (int i = 0; i < n - 1; i++)
g[U[i]].pb({ V[i], W[i] }),
g[V[i]].pb({ U[i], W[i] });
FU<VO(int, int)>build = [&](int a, int par)
{
ve<pii> kids;
each(it, g[a])
{
int b = it.f, c = it.s;
if (b == par) continue;
build(b, a);
kids.pb({ b, c });
}
g[a] = kids;
};
build(0, -1);
vell sol(n, 0);
for (int k = 0; k < n; k++)
{
vell dp0(n), dp1(n);
// 0 => are gradul <= k + 0
// 1 => are gradul <= k + 1
FU<VO(int)> dfs = [&](int a)
{
each(it, g[a])
{
dfs(it.f);
}
if (sz(g[a]) <= k - 1)
{
ll su = 0;
each(it, g[a]) su += min(0LL + dp1[it.first] + it.second, dp0[it.first]);
dp0[a] = dp1[a] = su;
return;
}
each(rem, vei({ k - 1, k }))
{
// sterg muchia => dp1 + cost
// pastrez muchia => dp0
int total = sz(g[a]);
int pastrez = min(total, rem);
ll su = 0;
each(it, g[a]) su += dp1[it.first] + it.second;
vector<ll> difs;
each(it, g[a]) difs.pb(min(0LL, dp0[it.first] - dp1[it.first] - it.second));
sort(difs.begin(), difs.end());
for (int i = 0; i < pastrez; i++)
{
assert(0 <= i && i < sz(difs));
su += difs[i];
}
if (rem == k - 1) dp0[a] = su; else dp1[a] = su;
}
assert(dp0[a] >= dp1[a]);
};
dfs(0);
sol[k] = dp1[0];
}
return sol;
}
# | 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... |