#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
vector<int> parents;
vector<vector<pair<int, int>>> graph;
int n, k;
void merge(priority_queue<int> &A, priority_queue<int> &B) {
if (A.size() > B.size()) A.swap(B);
while(A.size()) {B.push(A.top()); A.pop();}
}
priority_queue<int> dfs(int v, int p) {
priority_queue<int> res;
if (graph[v].size() == 1) res.push(0);
if (parents[v] != -1) return res;
parents[v] = p;
for (auto u: graph[v]) {
if (parents[u.first] != -1) continue;
priority_queue<int> tmp = dfs(u.first, v);
int newElem = tmp.top() + u.second;
tmp.pop();
tmp.push(newElem);
merge(tmp, res);
}
return res;
}
void solve() {
cin >> n >> k;
graph.resize(n);
parents.resize(n);
for (int i = 0; i<n-1; i++) {
int a, b, c; cin >> a >> b >> c;
a--; b--;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
for (int i = 0; i<n; i++) {
fill(parents.begin(), parents.end(), -1);
priority_queue<int> pq = dfs(i, -2);
int res = 0;
for (int i = 0; i<k; i++) {
if (pq.empty()) break;
res += pq.top();
pq.pop();
}
cout << res << "\n";
}
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
89 ms |
408 KB |
Output is correct |
9 |
Correct |
70 ms |
488 KB |
Output is correct |
10 |
Correct |
49 ms |
428 KB |
Output is correct |
11 |
Correct |
64 ms |
392 KB |
Output is correct |
12 |
Correct |
55 ms |
424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
89 ms |
408 KB |
Output is correct |
9 |
Correct |
70 ms |
488 KB |
Output is correct |
10 |
Correct |
49 ms |
428 KB |
Output is correct |
11 |
Correct |
64 ms |
392 KB |
Output is correct |
12 |
Correct |
55 ms |
424 KB |
Output is correct |
13 |
Correct |
475 ms |
524 KB |
Output is correct |
14 |
Correct |
340 ms |
684 KB |
Output is correct |
15 |
Correct |
162 ms |
576 KB |
Output is correct |
16 |
Correct |
282 ms |
460 KB |
Output is correct |
17 |
Correct |
269 ms |
540 KB |
Output is correct |
18 |
Correct |
210 ms |
448 KB |
Output is correct |
19 |
Correct |
497 ms |
540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1089 ms |
9868 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
89 ms |
408 KB |
Output is correct |
9 |
Correct |
70 ms |
488 KB |
Output is correct |
10 |
Correct |
49 ms |
428 KB |
Output is correct |
11 |
Correct |
64 ms |
392 KB |
Output is correct |
12 |
Correct |
55 ms |
424 KB |
Output is correct |
13 |
Correct |
475 ms |
524 KB |
Output is correct |
14 |
Correct |
340 ms |
684 KB |
Output is correct |
15 |
Correct |
162 ms |
576 KB |
Output is correct |
16 |
Correct |
282 ms |
460 KB |
Output is correct |
17 |
Correct |
269 ms |
540 KB |
Output is correct |
18 |
Correct |
210 ms |
448 KB |
Output is correct |
19 |
Correct |
497 ms |
540 KB |
Output is correct |
20 |
Execution timed out |
1089 ms |
9868 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |