This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 = 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});
active.insert(i + 1);
}
std::vector<ll> ans;
while (!active.empty()) {
ll root = *active.begin();
ll deepest = root, deepest_depth = 0;
std::function<void(ll, ll, ll)> dfs;
std::vector<std::pair<ll, ll>> up(n + 1);
dfs = [&](ll node, ll par, ll depth) {
if (depth > deepest_depth) {
deepest = node;
deepest_depth = 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(root, 0, 0);
ll dist = 0;
while (deepest != root 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 or !active.count(i)) {
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';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |