#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
long long f[N], g[N];
vector<pair<int, int> > adj[N];
int k;
void dfs(int u, int par = 0) {
long long sum = 0;
f[u] = g[u] = 0;
vector<long long> lst;
for(auto &[v, w] : adj[u]) if (v != par) {
dfs(v, u);
sum += g[v] + w;
lst.push_back(f[v] - g[v] - w);
}
sort(lst.begin(), lst.end());
for(int i = 0; i < min(k - 1, (int)lst.size()); i++) sum += min(0LL, lst[i]);
f[u] = sum;
if (k - 1 < lst.size()) sum += lst[k - 1];
g[u] = min(sum, f[u]);
// cout << u << " " << f[u] << " " << g[u] << endl;
}
vector<long long> minimum_closure_costs (int n, vector<int> u, vector<int> v, vector<int> w) {
for(int i = 0; i < n - 1; i++) {
adj[u[i]].emplace_back(v[i], w[i]);
adj[v[i]].emplace_back(u[i], w[i]);
}
vector<long long> ret(n);
for(k = 0; k < n; k++) {
dfs(0);
ret[k] = min(f[0], g[0]);
cout << ret[k] << endl;
}
return ret;
}
#ifdef ngu
int main() {
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
int n; cin >> n;
vector<int> u(n), v(n), w(n);
for(int i = 0; i < n; i++) cin >> u[i] >> v[i] >> w[i];
vector<long long> res = minimum_closure_costs(n, u, v, w);
}
#endif // ngu
Compilation message
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:23:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if (k - 1 < lst.size()) sum += lst[k - 1];
| ~~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2041 ms |
10792 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2041 ms |
10792 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |