#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const int MAXN = 100008;
vector<pair<int, ll>> edg[MAXN];
vector<ll> prof[MAXN];
vector<ll> merge(vector<ll>& a, vector<ll>& b) {
int n = a.size();
int m = b.size();
vector<ll> ans(n + m - 1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
ans[i + j] = max(ans[i + j], a[i] + b[j]);
}
}
return ans;
}
vector<ll> dfs(int v, int rt) {
if (edg[v].empty() || edg[v].size() == 1 && edg[v][0].first == rt) {
return {0, 0};
}
vector<vector<ll>> prof_edg;
for (auto [u, d] : edg[v]) {
if (u == rt)
continue;
prof_edg.push_back(dfs(u, v));
for (auto& x : prof_edg.back()) {
x += d;
}
prof_edg.back()[0] -= d;
}
while (prof_edg.size() > 1) {
int n = prof_edg.size();
vector<vector<ll>> new_profedg;
for (int i = 0; i + 1 < n; i += 2) {
new_profedg.push_back(merge(prof_edg[i], prof_edg[i + 1]));
}
if (n % 2) {
new_profedg.push_back(prof_edg.back());
}
prof_edg = new_profedg;
}
return prof_edg.back();
}
signed main() {
int n, k;
cin >> n >> k;
for (int i = 0; i + 1 < n; ++i) {
int u, v, c;
cin >> u >> v >> c;
--u, --v;
edg[u].push_back({v, c});
edg[v].push_back({u, c});
}
for (int i = 0; i < n; ++i) {
vector<ll> ans = dfs(i, -1);
if (k >= ans.size())
cout << ans.back() << '\n';
else
cout << ans[k] << '\n';
}
}
Compilation message
Main.cpp: In function 'std::vector<long long int> dfs(int, int)':
Main.cpp:23:46: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
23 | if (edg[v].empty() || edg[v].size() == 1 && edg[v][0].first == rt) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:62:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if (k >= ans.size())
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
10 ms |
4940 KB |
Output is correct |
4 |
Correct |
9 ms |
5060 KB |
Output is correct |
5 |
Correct |
11 ms |
4940 KB |
Output is correct |
6 |
Correct |
7 ms |
5020 KB |
Output is correct |
7 |
Correct |
10 ms |
5024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
10 ms |
4940 KB |
Output is correct |
4 |
Correct |
9 ms |
5060 KB |
Output is correct |
5 |
Correct |
11 ms |
4940 KB |
Output is correct |
6 |
Correct |
7 ms |
5020 KB |
Output is correct |
7 |
Correct |
10 ms |
5024 KB |
Output is correct |
8 |
Correct |
304 ms |
5148 KB |
Output is correct |
9 |
Correct |
272 ms |
5280 KB |
Output is correct |
10 |
Correct |
109 ms |
5104 KB |
Output is correct |
11 |
Correct |
344 ms |
5148 KB |
Output is correct |
12 |
Correct |
142 ms |
5104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
10 ms |
4940 KB |
Output is correct |
4 |
Correct |
9 ms |
5060 KB |
Output is correct |
5 |
Correct |
11 ms |
4940 KB |
Output is correct |
6 |
Correct |
7 ms |
5020 KB |
Output is correct |
7 |
Correct |
10 ms |
5024 KB |
Output is correct |
8 |
Correct |
304 ms |
5148 KB |
Output is correct |
9 |
Correct |
272 ms |
5280 KB |
Output is correct |
10 |
Correct |
109 ms |
5104 KB |
Output is correct |
11 |
Correct |
344 ms |
5148 KB |
Output is correct |
12 |
Correct |
142 ms |
5104 KB |
Output is correct |
13 |
Execution timed out |
1081 ms |
5196 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
12144 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
10 ms |
4940 KB |
Output is correct |
4 |
Correct |
9 ms |
5060 KB |
Output is correct |
5 |
Correct |
11 ms |
4940 KB |
Output is correct |
6 |
Correct |
7 ms |
5020 KB |
Output is correct |
7 |
Correct |
10 ms |
5024 KB |
Output is correct |
8 |
Correct |
304 ms |
5148 KB |
Output is correct |
9 |
Correct |
272 ms |
5280 KB |
Output is correct |
10 |
Correct |
109 ms |
5104 KB |
Output is correct |
11 |
Correct |
344 ms |
5148 KB |
Output is correct |
12 |
Correct |
142 ms |
5104 KB |
Output is correct |
13 |
Execution timed out |
1081 ms |
5196 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |