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 <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, k;
vector<pair<int, int>> adj[MAXN];
multiset<long long> st;
long long mx[MAXN];
void dfs(int u, int p) {
mx[u] = 0;
bool leaf = 1;
for (auto [v, w] : adj[u]) {
if (v == p) continue;
leaf = 0;
dfs(v, u);
st.erase(st.find(mx[v]));
st.insert(mx[v] + w);
mx[u] = max(mx[u], mx[v] + w);
}
if (leaf) {
st.insert(0);
}
}
int main() {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int a, b, c; cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
for (int i = 1; i <= n; i++) {
st.clear();
dfs(i, -1);
long long ans = 0;
while (st.size() > k) {
st.erase(st.begin());
}
for (long long x : st) {
ans += x;
}
cout << ans << '\n';
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:38:26: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
38 | while (st.size() > k) {
| ~~~~~~~~~~^~~
# | 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... |