이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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';
}
# | 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... |