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 endl '\n'
//#define int long long
//#define INT_MAX LONG_LONG_MAX
//satiating my crippling points addiction
long long findscore(multiset<pair<long long, pair<long long, long long>>>& candidates, int k, int j){
long long score = 0;
for (auto i : candidates){
if (j <= k){
score += min(i.second.first, i.second.second);
}
else{
j--;
score += i.second.second;
}
}
return score;
}
long long solve(vector<vector<long long>>& dp, vector<vector<pair<int, int>>>& adj, int p, int n, int k, int i, int j){
if (dp[i][j] != -1){
return dp[i][j];
}
bool ac = true;
multiset<pair<long long, pair<long long, long long>>> candidates;
for (auto a : adj[i]){
if (a.first != p){
ac = false;
long long op1 = solve(dp, adj, i, n, k, a.first, 0);
long long op2 = solve(dp, adj, i, n, k, a.first, 1) + a.second;
candidates.insert({op2 - op1, {op1, op2}});
}
}
if (ac){
return 0;
}
return dp[i][j] = findscore(candidates, k, adj[i].size() - j);
}
vector<long long> minimum_closure_costs(int n, vector<int> u, vector<int> v, vector<int> w){
bool subtask1 = true;
for (int i = 1; i < u.size(); ++i){
if (u[i] != u[i - 1]){
subtask1 = false;
break;
}
}
vector<long long> out(n);
if (subtask1){
sort(w.begin(), w.end());
int j = 0;
for (int i = n - 2; i >= 0; --i){
out[i] = out[i + 1] + w[j];
j++;
}
return out;
}
bool subtask2 = true;
for (int i = 0; i < n - 1; ++i){
if (!(u[i] == i && v[i] == i + 1)){
subtask2 = false;
break;
}
}
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n - 1; ++i){
adj[u[i]].push_back({v[i], w[i]});
adj[v[i]].push_back({u[i], w[i]});
}
if (subtask2){
vector<vector<long long>> dp(n + 10, vector<long long>(2, -1));
out[0] = solve(dp, adj, -1, n, 0, 0, 0);
vector<vector<long long>> dp2(n + 10, vector<long long>(2, -1));
out[1] = solve(dp2, adj, -1, n, 1, 0, 0);
return out;
}
for (int i = 0; i < n; ++i){
vector<vector<long long>> dp(n + 10, vector<long long>(2, -1));
out[i] = solve(dp, adj, -1, n, i, 0, 0);
}
return out;
}
/*
int main(){
int n;
cin >> n;
vector<int> u(n - 1), v(n - 1), w(n - 1);
for (int i = 0; i < n - 1; ++i){
cin >> u[i];
}
for (int i = 0; i < n - 1; ++i){
cin >> v[i];
}
for (int i = 0; i < n - 1; ++i){
cin >> w[i];
}
vector<long long> out;
out = minimum_closure_costs(n, u, v, w);
for (auto a : out){
cout << a << " ";
}
return 0;
}
*/
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:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 1; 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... |