이 제출은 이전 버전의 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::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... |