#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
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) {
| ~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
7 ms |
2644 KB |
Output is correct |
4 |
Correct |
6 ms |
2644 KB |
Output is correct |
5 |
Correct |
7 ms |
2672 KB |
Output is correct |
6 |
Correct |
5 ms |
2644 KB |
Output is correct |
7 |
Correct |
6 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
7 ms |
2644 KB |
Output is correct |
4 |
Correct |
6 ms |
2644 KB |
Output is correct |
5 |
Correct |
7 ms |
2672 KB |
Output is correct |
6 |
Correct |
5 ms |
2644 KB |
Output is correct |
7 |
Correct |
6 ms |
2644 KB |
Output is correct |
8 |
Correct |
174 ms |
2716 KB |
Output is correct |
9 |
Correct |
119 ms |
2772 KB |
Output is correct |
10 |
Correct |
107 ms |
2700 KB |
Output is correct |
11 |
Correct |
157 ms |
2732 KB |
Output is correct |
12 |
Correct |
127 ms |
2712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
7 ms |
2644 KB |
Output is correct |
4 |
Correct |
6 ms |
2644 KB |
Output is correct |
5 |
Correct |
7 ms |
2672 KB |
Output is correct |
6 |
Correct |
5 ms |
2644 KB |
Output is correct |
7 |
Correct |
6 ms |
2644 KB |
Output is correct |
8 |
Correct |
174 ms |
2716 KB |
Output is correct |
9 |
Correct |
119 ms |
2772 KB |
Output is correct |
10 |
Correct |
107 ms |
2700 KB |
Output is correct |
11 |
Correct |
157 ms |
2732 KB |
Output is correct |
12 |
Correct |
127 ms |
2712 KB |
Output is correct |
13 |
Execution timed out |
694 ms |
2800 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1095 ms |
9460 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
7 ms |
2644 KB |
Output is correct |
4 |
Correct |
6 ms |
2644 KB |
Output is correct |
5 |
Correct |
7 ms |
2672 KB |
Output is correct |
6 |
Correct |
5 ms |
2644 KB |
Output is correct |
7 |
Correct |
6 ms |
2644 KB |
Output is correct |
8 |
Correct |
174 ms |
2716 KB |
Output is correct |
9 |
Correct |
119 ms |
2772 KB |
Output is correct |
10 |
Correct |
107 ms |
2700 KB |
Output is correct |
11 |
Correct |
157 ms |
2732 KB |
Output is correct |
12 |
Correct |
127 ms |
2712 KB |
Output is correct |
13 |
Execution timed out |
694 ms |
2800 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |