#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md ((l + r) >> 1)
pair<ll, int> seg[1 << 18];
void build(int i, int l, int r) {
if (l == r) {
seg[i] = {-1e18, l};
return;
}
build(i << 1, l, md);
build(i << 1 | 1, md + 1, r);
seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}
void upd(int i, int l, int r, int x, int t, ll v) {
if (l == x && x == r) {
if (t == 0) {
seg[i].first += v;
} else {
seg[i].first = v;
}
return;
}
if (x <= md) {
upd(i << 1, l, md, x, t, v);
} else {
upd(i << 1 | 1, md + 1, r, x, t, v);
}
seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}
pair<ll, int> get(int i, int l, int r, int s, int e) {
if (s <= l && r <= e) {
return seg[i];
}
if (r < s || e < l) {
return {-1e18, l};
}
return max(get(i << 1, l, md, s, e), get(i << 1 | 1, md + 1, r, s, e));
}
const int N = 100'005;
vector<array<int, 2>> adj[N];
int n, k, st[N], en[N], t;
void dfs(int i, int par, int parc) {
st[i] = t++;
for (auto [nxt, w] : adj[i]) {
if (nxt == par) continue;
dfs(nxt, i, w);
}
en[i] = t;
if (adj[i].size() == 1) {
upd(1, 1, n, st[i], 1, 0);
}
pair<ll, int> val = get(1, 1, n, st[i], en[i]);
upd(1, 1, n, val.second, 0, parc);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
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++) {
build(1, 1, n);
t = 1;
dfs(i, 0, 0);
ll sum = 0;
int cnt = k;
while (cnt--) {
pair<ll, int> val = get(1, 1, n, 1, n);
sum += val.first;
upd(1, 1, n, val.second, 1, -1e18);
}
cout << sum << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
9 ms |
4444 KB |
Output is correct |
4 |
Correct |
9 ms |
4444 KB |
Output is correct |
5 |
Correct |
10 ms |
4444 KB |
Output is correct |
6 |
Correct |
9 ms |
4444 KB |
Output is correct |
7 |
Correct |
9 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
9 ms |
4444 KB |
Output is correct |
4 |
Correct |
9 ms |
4444 KB |
Output is correct |
5 |
Correct |
10 ms |
4444 KB |
Output is correct |
6 |
Correct |
9 ms |
4444 KB |
Output is correct |
7 |
Correct |
9 ms |
4444 KB |
Output is correct |
8 |
Correct |
252 ms |
4440 KB |
Output is correct |
9 |
Correct |
236 ms |
4648 KB |
Output is correct |
10 |
Correct |
202 ms |
4444 KB |
Output is correct |
11 |
Correct |
270 ms |
4440 KB |
Output is correct |
12 |
Correct |
222 ms |
4848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
9 ms |
4444 KB |
Output is correct |
4 |
Correct |
9 ms |
4444 KB |
Output is correct |
5 |
Correct |
10 ms |
4444 KB |
Output is correct |
6 |
Correct |
9 ms |
4444 KB |
Output is correct |
7 |
Correct |
9 ms |
4444 KB |
Output is correct |
8 |
Correct |
252 ms |
4440 KB |
Output is correct |
9 |
Correct |
236 ms |
4648 KB |
Output is correct |
10 |
Correct |
202 ms |
4444 KB |
Output is correct |
11 |
Correct |
270 ms |
4440 KB |
Output is correct |
12 |
Correct |
222 ms |
4848 KB |
Output is correct |
13 |
Execution timed out |
1067 ms |
4648 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1010 ms |
11344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
9 ms |
4444 KB |
Output is correct |
4 |
Correct |
9 ms |
4444 KB |
Output is correct |
5 |
Correct |
10 ms |
4444 KB |
Output is correct |
6 |
Correct |
9 ms |
4444 KB |
Output is correct |
7 |
Correct |
9 ms |
4444 KB |
Output is correct |
8 |
Correct |
252 ms |
4440 KB |
Output is correct |
9 |
Correct |
236 ms |
4648 KB |
Output is correct |
10 |
Correct |
202 ms |
4444 KB |
Output is correct |
11 |
Correct |
270 ms |
4440 KB |
Output is correct |
12 |
Correct |
222 ms |
4848 KB |
Output is correct |
13 |
Execution timed out |
1067 ms |
4648 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |