#include <bits/stdc++.h>
typedef long long ll;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, k;
std::cin >> n >> k;
std::vector<std::vector<std::pair<ll, ll>>> adj(n + 1);
std::set<ll> active;
for (ll i = 1; i <= n; ++i) {
active.insert(i);
}
for (ll i = 0, u, v, w; i < n - 1; ++i) {
std::cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
std::function<void(ll, ll, ll)> dfs;
std::vector<std::pair<ll, ll>> up(n + 1);
std::vector<ll> depths(n + 1);
dfs = [&](ll node, ll par, ll depth) {
depths[node] = depth;
for (auto &[i, wt] : adj[node]) {
if (i == par or !active.count(i)) {
continue;
}
up[i] = {node, wt};
dfs(i, node, depth + wt);
}
};
dfs(1, 0, 0);
std::priority_queue<std::pair<ll, ll>> pq;
for (ll i = 1; i <= n; ++i) {
pq.push({depths[i], i});
}
std::vector<ll> ans;
while (!pq.empty()) {
ll deepest = pq.top().second;
pq.pop();
if (!active.count(deepest)) {
continue;
}
ll dist = 0;
while (deepest != 1 and dist + up[deepest].second <= k) {
dist += up[deepest].second;
deepest = up[deepest].first;
}
ans.push_back(deepest);
std::function<void(ll, ll, ll)> dfs2;
dfs2 = [&](ll node, ll par, ll depth) {
if (depth > k) {
return;
}
active.erase(node);
for (auto &[i, wt] : adj[node]) {
if (i == par) {
continue;
}
dfs2(i, node, depth + wt);
}
};
dfs2(deepest, 0, 0);
}
std::cout << ans.size() << '\n';
for (auto &i : ans) {
std::cout << i << ' ';
}
std::cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
710 ms |
64604 KB |
Output is correct |
2 |
Correct |
701 ms |
64572 KB |
Output is correct |
3 |
Correct |
195 ms |
23500 KB |
Output is correct |
4 |
Correct |
656 ms |
62800 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
770 ms |
64584 KB |
Output is correct |
2 |
Correct |
344 ms |
32732 KB |
Output is correct |
3 |
Correct |
360 ms |
33472 KB |
Output is correct |
4 |
Correct |
297 ms |
29688 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
356 KB |
Output is correct |
7 |
Execution timed out |
3080 ms |
56224 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
860 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
99 ms |
1116 KB |
Output is correct |
6 |
Correct |
76 ms |
1116 KB |
Output is correct |
7 |
Correct |
50 ms |
1116 KB |
Output is correct |
8 |
Correct |
54 ms |
1112 KB |
Output is correct |
9 |
Correct |
63 ms |
1160 KB |
Output is correct |
10 |
Correct |
47 ms |
1112 KB |
Output is correct |
11 |
Correct |
90 ms |
1112 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
53 ms |
1116 KB |
Output is correct |
14 |
Correct |
54 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
856 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
4 ms |
856 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
844 ms |
57804 KB |
Output is correct |
2 |
Correct |
492 ms |
55720 KB |
Output is correct |
3 |
Correct |
566 ms |
59392 KB |
Output is correct |
4 |
Correct |
192 ms |
28096 KB |
Output is correct |
5 |
Execution timed out |
3033 ms |
70848 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |