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;
const long long MAXN = 1e5 + 5;
vector<array<long long, 2> > g[MAXN];
long long n, k;
vector<array<long long, 2> > dp(MAXN);
// dp[node][0] -- parent weight not selected
// dp[node][1] -- parent weight selected
void dfs(long long node, long long par, long long par_wt)
{
// if(k == 1) cout << "node = " << node << ", par = " << par << ", par_wt = " << par_wt << "\n";
if(g[node].size() == 1 && par != -1) // base case
{
dp[node][0] = 0;
dp[node][1] = par_wt;
return;
}
long long sum = 0;
vector<long long> diff;
for(auto [to, wt]: g[node])
{
if(to != par)
{
dfs(to, node, wt);
sum += dp[to][0];
diff.push_back(dp[to][1] - dp[to][0]);
}
}
sort(diff.begin(), diff.end());
reverse(diff.begin(), diff.end());
long long gg = diff.size();
for(long long i = diff.size() - 1; i >= 0; i--)
{
if(diff[i] < 0)
{
gg = i;
}
}
// if(k == 1) cout << "node = " << node << ", diff.size() = " << diff.size() << "\n";
long long children_siz = g[node].size() - 1;
// no. of children + parent <= k
if(children_siz + 1 <= k)
{
for(long long i = diff.size() - 1; i >= gg; i--)
{
sum += diff[i];
}
dp[node][0] = sum;
dp[node][1] = sum + par_wt;
return;
}
// if(k == 1) cout << "node has reached here\n";
// keeping edges between k - 1 children and parent
long long buffer_value = 0;
for(long long i = diff.size() - 1; i >= min(gg, k - 1); i--)
{
buffer_value += diff[i];
}
dp[node][0] = sum + buffer_value;
// keeping edges between k children only
buffer_value = par_wt;
for(long long i = diff.size() - 1; i >= min(k, gg); i--)
{
buffer_value += diff[i];
}
dp[node][1] = sum + buffer_value;
}
long long calculatedp()
{
for(long long i = 0; i <= n; i++)
{
dp[i][0] = 0;
dp[i][1] = 0;
}
dfs(0, -1, 0);
if(k == 1)
{
// cout << "dp values\n";
// for(int i = 0; i < n; i++)
// {
// cout << dp[i][0] << " " << dp[i][1] << "\n";
// }
}
return min(dp[0][0], dp[0][1]);
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W)
{
n = N;
long long sum = 0;
for(long long i = 0; i < U.size(); i++)
{
sum += W[i];
// cout << "U[i] = " << U[i] << ", V[i] = " << V[i] << ", W[i] = " << W[i] << "\n";
g[U[i]].push_back({V[i], W[i]});
g[V[i]].push_back({U[i], W[i]});
}
vector<long long> ans;
ans.push_back(sum);
for(long long i = 1; i < N; i++)
{
k = i;
ans.push_back(calculatedp());
}
return ans;
}
Compilation message (stderr)
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:95:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(long long i = 0; i < U.size(); i++)
| ~~^~~~~~~~~~
# | 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... |