#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;
vector<pair<int, int> > adj[N];
deque<long long> f[N];
long long sz[N], w[N];
void dfs(int u, int par = 0) {
if (adj[u].size() == 1) {
f[u].push_back(0);
return;
}
vector<int> child;
for(auto &[v, cost] : adj[u]) if (v != par) {
dfs(v, u);
sz[u] += sz[v] + cost;
w[v] = cost;
child.push_back(v);
}
sort(child.begin(), child.end(), [] (const int &x, const int &y) {
return f[x].size() > f[y].size();
});
f[u] = f[child[0]];
while (f[u].size() <= child.size()) f[u].push_back(0);
int heavy = child[0];
f[u].push_front(sz[u]);
child.erase(child.begin());
for(int &v : child) {
for(int k = 1; k <= f[v].size(); k++) f[u][k] += f[v][k - 1];
}
for(int k = 1; k <= child.size(); k++) {
vector<int> lst;
if (f[heavy].size() > k) lst.push_back(f[heavy][k] + w[heavy] - f[heavy][k - 1]);
else lst.push_back(w[heavy]);
for(int &v : child) {
if (k < f[v].size()) lst.push_back(f[v][k] + w[v] - f[v][k - 1]);
else lst.push_back(w[v]);
}
sort(lst.begin(), lst.end());
// for(int &j : lst) cout << j << " "; cout << endl;
// cout << child.size() - k + 1 << endl;
for(int i = 0; i < lst.size(); i++) {
// cout << i << " " << child.size() - k + 1 << " " << lst[i] << endl;
if (i < child.size() - k + 1 || lst[i] < 0) f[u][k] += lst[i];
}
// cout << f[u][k] << " " << k << endl;
}
// cout << f[u][2] << endl;
}
vector<long long> minimum_closure_costs (int n, vector<int> u, vector<int> v, vector<int> w) {
vector<long long> ret;
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]);
}
dfs(0);
while (f[0].size() < n) f[0].push_back(0);
for(int i = 0; i < n; i++) ret.push_back(f[0][i]);
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);
for(long long &j : res) cout << j << endl;
}
#endif // ngu
Compilation message
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:32:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int k = 1; k <= f[v].size(); k++) f[u][k] += f[v][k - 1];
| ~~^~~~~~~~~~~~~~
roads.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int k = 1; k <= child.size(); k++) {
| ~~^~~~~~~~~~~~~~~
roads.cpp:36:29: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
36 | if (f[heavy].size() > k) lst.push_back(f[heavy][k] + w[heavy] - f[heavy][k - 1]);
| ~~~~~~~~~~~~~~~~^~~
roads.cpp:39:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if (k < f[v].size()) lst.push_back(f[v][k] + w[v] - f[v][k - 1]);
| ~~^~~~~~~~~~~~~
roads.cpp:45:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0; i < lst.size(); i++) {
| ~~^~~~~~~~~~~~
roads.cpp:47:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if (i < child.size() - k + 1 || lst[i] < 0) f[u][k] += lst[i];
| ~~^~~~~~~~~~~~~~~~~~~~~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:61:24: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
61 | while (f[0].size() < n) f[0].push_back(0);
| ~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
69888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
69972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
69972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
69972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
115 ms |
78944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
115 ms |
78944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
69888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |