CSES - Shared codeLink to this code:
https://cses.fi/paste/2f989b52ad9c82a430051a/
//PPAP_1264589
#include <bits/stdc++.h>
#define Task "A"
#define up(i,a,b) for (int i = a; i <= b; i++)
#define ep emplace_back
using namespace std;
const int maxn = 1e5 + 10;
vector<int> a[maxn];
int tr[maxn];
int existed[maxn];
int dp[maxn];
int n,m;
void in(){
cin >> n >> m;
up(i,1,m){
int u,v;
cin >> u >> v;
if (existed[u] == v) continue;
existed[u] = v;
a[u].ep(v);
}
}
queue<int> Q;
void BFS(int root){
Q.push(root);
while (!Q.empty()){
int u = Q.front();
for (int v : a[u]){
if (dp[v] < dp[u] + 1){
dp[v] = dp[u] + 1;
tr[v] = u;
Q.push(v);
}
}
Q.pop();
}
}
void Fpath(){
int x = n;
vector<int> path;
while (x != 1){
path.ep(x);
x = tr[x];
}
path.ep(1);
reverse(path.begin(), path.end());
for (auto x : path) cout << x << " ";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen(Task".inp", "r")){
freopen (Task".inp", "r", stdin);
freopen (Task".out", "w", stdout);
}
in();
dp[1] = 1;
BFS(1);
if (dp[n] == 0){
cout << "IMPOSSIBLE";
return 0;
}
cout << dp[n] << "\n";
Fpath();
}
// Algorithmic logic