CSES - Shared codeLink to this code:
https://cses.fi/paste/ce846cad17206e7c300108/
//PPAP_1264589
#include <bits/stdc++.h>
#define Task "A"
#define up(i,a,b) for (int i = a; i <= b; i++)
#define down(i,a,b) for (int i = a; i >= b; i--)
#define pii pair<int, int>
#define f first
#define s second
#define ep emplace_back
#define bit(x,i) ((x >> i) & 1)
using namespace std;
const int LIM = 1010580540;
const int MOD = 1e9 + 7;
const int maxn = 1e5 + 10;
int dp[maxn];
int check[maxn];
int tr[maxn];
vector<int> a[maxn];
int n,m;
void in(){
cin >> n >> m;
up(i,1,m){
int u,v;
cin >> u >> v;
if (check[u] == v) continue;
check[u] = v;
a[u].ep(v);
}
}
void DFS(int u){
for (int v : a[u]){
DFS(v);
if (dp[v]){
if (dp[u] < dp[v] + 1){
dp[u] = dp[v] + 1;
tr[u] = v;
}
}
}
}
void Fpath(){
int x = 1;
vector<int> path;
while (x != n){
path.ep(x);
x = tr[x];
}
path.ep(n);
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[n] = 1;
DFS(1);
if (dp[1] == 0) cout << "IMPOSSIBLE";
else {
cout << dp[1] << "\n";
Fpath();
}
}
// Algorithmic logic