Problem Statement
Message Route
Implementation
void solve() {
ll n, m;
cin >> n >> m;
vvll v(n);
rep(i, m) {
ll x, y;
cin >> x >> y;
--x, --y;
v[x].pb(y);
v[y].pb(x);
}
vll save(n, -1), vis(n, 0);
function<void(ll) > bfs = [&](ll node) {
queue<ll> q;
q.push(node);
while (sz(q)) {
auto cur = q.front();
q.pop();
vis[cur] = 1;
for (auto& child : v[cur]) {
if (!vis[child]) {
q.push(child);
vis[child] = 1;
save[child] = cur;
}
}
}
};
bfs(0);
if (vis[n - 1]) {
vll path;
for (ll st = n - 1; st != 0; st = save[st])
path.pb(st);
path.pb(0);
reverse(all(path));
cout << sz(path) << "\n";
rep(i, sz(path)) cout << path[i] + 1 << " ";
cout << "\n";
}
else cout << "IMPOSSIBLE\n";
}