#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define ll long long
const int N = 100'005;
multiset<ll> s[N];
vector<array<int, 2>> adj[N];
void dfs(int i, int par, int parc) {
for (auto [nxt, w] : adj[i]) {
if (nxt == par) continue;
dfs(nxt, i, w);
if (s[i] < s[nxt]) {
swap(s[i], s[nxt]);
}
for (auto j : s[nxt]) {
s[i].insert(j);
}
}
if (adj[i].size() == 1) s[i].insert(0);
ll val = *s[i].begin();
s[i].erase(s[i].begin());
s[i].insert(val - parc);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1, u, v, w; i < n; i++) {
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
s[j].clear();
}
dfs(i, 0, 0);
ll sum = 0;
int cnt = 0;
for (auto j : s[i]) {
if (cnt == k) break;
sum += j;
cnt++;
}
cout << -sum << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
19 ms |
7512 KB |
Output is correct |
4 |
Correct |
21 ms |
7516 KB |
Output is correct |
5 |
Correct |
65 ms |
7772 KB |
Output is correct |
6 |
Correct |
7 ms |
7516 KB |
Output is correct |
7 |
Correct |
11 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
19 ms |
7512 KB |
Output is correct |
4 |
Correct |
21 ms |
7516 KB |
Output is correct |
5 |
Correct |
65 ms |
7772 KB |
Output is correct |
6 |
Correct |
7 ms |
7516 KB |
Output is correct |
7 |
Correct |
11 ms |
7516 KB |
Output is correct |
8 |
Execution timed out |
1074 ms |
8688 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
19 ms |
7512 KB |
Output is correct |
4 |
Correct |
21 ms |
7516 KB |
Output is correct |
5 |
Correct |
65 ms |
7772 KB |
Output is correct |
6 |
Correct |
7 ms |
7516 KB |
Output is correct |
7 |
Correct |
11 ms |
7516 KB |
Output is correct |
8 |
Execution timed out |
1074 ms |
8688 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1039 ms |
275624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
19 ms |
7512 KB |
Output is correct |
4 |
Correct |
21 ms |
7516 KB |
Output is correct |
5 |
Correct |
65 ms |
7772 KB |
Output is correct |
6 |
Correct |
7 ms |
7516 KB |
Output is correct |
7 |
Correct |
11 ms |
7516 KB |
Output is correct |
8 |
Execution timed out |
1074 ms |
8688 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |