This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://atcoder.jp/contests/abc270/tasks/abc270_c
// competitive-verifier: IGNORE
#include <iostream>
#include <ranges>
#include "tools/join.hpp"
#include "tools/zero_one_bfs.hpp"
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int N, X, Y;
std::cin >> N >> X >> Y;
--X, --Y;
tools::zero_one_bfs<false, int> graph(N);
for (int i = 0; i < N - 1; ++i) {
int U, V;
std::cin >> U >> V;
--U, --V;
graph.add_edge(U, V, 1);
}
std::cout << tools::join(graph.query<true>(X).vertex_path(Y) | std::views::transform([](const auto v) { return v + 1; }), " ") << '\n';
return 0;
}
#line 1 "tests/zero_one_bfs/undirected.test.cpp"
// competitive-verifier: PROBLEM https://atcoder.jp/contests/abc270/tasks/abc270_c
// competitive-verifier: IGNORE
#include <iostream>
#include <ranges>
#line 1 "tools/join.hpp"
#line 5 "tools/join.hpp"
#include <sstream>
namespace tools {
template <::std::ranges::range R, typename T>
::std::string join(R&& e, const T& d) {
::std::ostringstream ss;
auto it = ::std::ranges::begin(e);
const auto end = ::std::ranges::end(e);
if (it != end) {
ss << *it;
for (++it; it != end; ++it) {
ss << d << *it;
}
}
return ss.str();
}
}
#line 1 "tools/zero_one_bfs.hpp"
#include <algorithm>
#include <cassert>
#include <deque>
#include <iterator>
#include <limits>
#include <utility>
#include <tuple>
#include <vector>
#line 1 "tools/chmin.hpp"
#include <type_traits>
#line 6 "tools/chmin.hpp"
namespace tools {
template <typename M, typename N>
bool chmin(M& lhs, const N& rhs) {
bool updated;
if constexpr (::std::is_integral_v<M> && ::std::is_integral_v<N>) {
updated = ::std::cmp_less(rhs, lhs);
} else {
updated = rhs < lhs;
}
if (updated) lhs = rhs;
return updated;
}
}
#line 1 "tools/shortest_path_tree.hpp"
#line 10 "tools/shortest_path_tree.hpp"
namespace tools {
template <typename Cost, typename F>
class shortest_path_tree {
::std::vector<Cost> m_dist;
::std::vector<int> m_from;
F m_get_vertex;
public:
shortest_path_tree() = default;
template <::std::ranges::range R1, ::std::ranges::range R2>
shortest_path_tree(R1&& d, R2&& p, const F& f) : m_get_vertex(f) {
::std::ranges::copy(d, ::std::back_inserter(this->m_dist));
::std::ranges::copy(p, ::std::back_inserter(this->m_from));
assert(this->m_dist.size() == this->m_from.size());
assert(::std::ranges::all_of(this->m_from, [](const auto p_i) { return p_i >= -1; }));
}
int size() const {
return this->m_dist.size();
}
const ::std::vector<Cost>& dist() const & {
return this->m_dist;
}
::std::vector<Cost> dist() && {
return ::std::move(this->m_dist);
}
Cost dist(const int v) const {
assert(0 <= v && v < this->size());
return this->m_dist[v];
}
int from_vertex(const int v) const {
assert(0 <= v && v < this->size());
return this->m_from[v] >= 0 ? this->m_get_vertex(this->m_from[v], v) : -1;
}
int from_edge_id(const int v) const {
assert(0 <= v && v < this->size());
return this->m_from[v];
}
::std::vector<int> vertex_path(const int v) const {
assert(0 <= v && v < this->size());
::std::vector<int> path;
for (int u = v; u >= 0; u = this->from_vertex(u)) {
path.push_back(u);
}
::std::ranges::reverse(path);
return path;
}
::std::vector<int> edge_id_path(const int v) const {
assert(0 <= v && v < this->size());
::std::vector<int> path;
for (int u = v; this->m_from[u] >= 0; u = this->from_vertex(u)) {
path.push_back(this->m_from[u]);
}
::std::ranges::reverse(path);
return path;
}
};
template <::std::ranges::range R1, ::std::ranges::range R2, typename F>
shortest_path_tree(R1&&, R2&&, const F&) -> shortest_path_tree<::std::ranges::range_value_t<R1>, F>;
}
#line 14 "tools/zero_one_bfs.hpp"
namespace tools {
template <bool Directed, typename T>
class zero_one_bfs {
public:
struct edge {
int from;
int to;
T cost;
};
private:
::std::vector<edge> m_edges;
::std::vector<::std::vector<int>> m_graph;
public:
zero_one_bfs() = default;
explicit zero_one_bfs(const int n) : m_graph(n) {
}
int size() const {
return this->m_graph.size();
}
int add_edge(int u, int v, const T w) {
assert(0 <= u && u < this->size());
assert(0 <= v && v < this->size());
assert(w == 0 || w == 1);
if constexpr (!Directed) {
::std::tie(u, v) = ::std::minmax({u, v});
}
this->m_edges.push_back({u, v, w});
this->m_graph[u].push_back(this->m_edges.size() - 1);
if constexpr (!Directed) {
this->m_graph[v].push_back(this->m_edges.size() - 1);
}
return this->m_edges.size() - 1;
}
const edge& get_edge(const int k) const & {
assert(0 <= k && k < ::std::ssize(this->m_edges));
return this->m_edges[k];
}
edge get_edge(const int k) && {
assert(0 <= k && k < ::std::ssize(this->m_edges));
return ::std::move(this->m_edges[k]);
}
const ::std::vector<edge>& edges() const & {
return this->m_edges;
}
::std::vector<edge> edges() && {
return ::std::move(this->m_edges);
}
template <bool Restore = false>
auto query(const int s) const {
assert(0 <= s && s < this->size());
::std::vector<T> dist(this->size(), ::std::numeric_limits<T>::max());
dist[s] = 0;
::std::vector<int> prev(Restore ? this->size() : 0, -1);
::std::deque<::std::pair<int, T>> deque;
deque.emplace_front(s, 0);
while (!deque.empty()) {
const auto [here, d] = deque.front();
deque.pop_front();
if (dist[here] < d) continue;
for (const auto edge_id : this->m_graph[here]) {
const auto& edge = this->m_edges[edge_id];
const auto next = edge.to ^ (Directed ? 0 : edge.from ^ here);
if (::tools::chmin(dist[next], dist[here] + edge.cost)) {
if constexpr (Restore) {
prev[next] = edge.id;
}
if (edge.cost == 0) {
deque.emplace_front(next, dist[next]);
} else {
deque.emplace_back(next, dist[next]);
}
}
}
}
if constexpr (Restore) {
return ::tools::shortest_path_tree(dist, prev, [&](const auto e, const auto v) {
return this->m_edges[e].from ^ (Directed ? 0 : this->m_edges[e].to ^ v);
});
} else {
return dist;
}
}
};
}
#line 8 "tests/zero_one_bfs/undirected.test.cpp"
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int N, X, Y;
std::cin >> N >> X >> Y;
--X, --Y;
tools::zero_one_bfs<false, int> graph(N);
for (int i = 0; i < N - 1; ++i) {
int U, V;
std::cin >> U >> V;
--U, --V;
graph.add_edge(U, V, 1);
}
std::cout << tools::join(graph.query<true>(X).vertex_path(Y) | std::views::transform([](const auto v) { return v + 1; }), " ") << '\n';
return 0;
}