CSES - Shared codeLink to this code:
https://cses.fi/paste/9ee3d2f988dc939e6047e1/
//Problem Link -
/* By Shark08*/
#include<bits/stdc++.h>
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/trie_policy.hpp>*/
// using namespace __gnu_pbds;
using namespace std;
#define ll long long int
#define ld long double
#define mod 1000000007
#define inf 1e9
#define endl "\n"
#define pb push_back
#define vi vector<ll>
#define vs vector<ss>
#define pii pair<ll,ll>
#define ump unordered_map
#define mp make_pair
#define pq_max priority_queue<ll,ll>
#define pq_min priority_queue<ll,vector<ll>,greater<ll> >
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define mid(l,r) (l+(r-l)/2)
#define bitc(x) __buildtin_popcount(x)
#define loop(i,a,b) for(int i=a ; i<=b ; i++)
#define looprev(i,a,b) for(int i=a;i>=b;i--)
#define iter(c,it) for(__typeof(c.begin()) it =c.begin() ; it!=c.end();it++)
#define log(args...) {string _s = #args; replace(_s.begin(), _s.end(), ',',' ' ); stringstream __ss(_s); istream_iterator<string> _it(__ss) ; err(_it, args); }
#define logarr(arr,a,b) for(int z=a;z<=b;z++) cout<<(arr[z])<<" "; cout<<endl;
template<typename T> T gcd(T a, T b){ if(a%b) return gcd(b, a%b) ; return b;}
template<typename T> T lcm(T a ,T b) { return (a*(b/gcd(a,b)));}
void err(istream_iterator<string> it){}
template <typename T,typename... Args>
void err (istream_iterator<string> it, T a, Args... args){
cout<< *it <<" = "<< a << endl;
err(++it,args...);
}
//
//
void file_i_o()
{
ios_base::sync_with_stdio(false);
//untie
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
const int N =1e6;
vector<vector<ll>> Adj(N,vector<ll>());
vector<ll> Par(N,-1);
vector<ll> Deg(N,0);
void topo(queue<ll> &Q)
{
while(!Q.empty())
{
int x = Q.front();
Q.pop();
for(ll t : Adj[x])
{
Deg[t]--;
if(Deg[t] == 0)
{
Par[t] = x;
if(t != 1)
{
Q.push(t);
}
}
}
}
}
int main(int argc, char const *argv[])
{
/* code */
clock_t begin = clock();
file_i_o();
ll n,m;
cin>>n>>m;
ll a,b;
for(ll i= 1; i<=n ; i++)
{
Deg[i] = 0;
Par[i] =-1;
}
loop(i,0,m-1)
{
cin>>a>>b;
Adj[a].pb(b);
Deg[b]++;
}
queue<ll> Q;
for(ll i= 2; i<=n;i++)
{
if(Deg[i] == 0)
{
Q.push(i);
}
}
topo(Q);
Q.push(1);
Par[n] = -1;
Par[1] = -1;
topo(Q);
if(Par[n] == -1)
{
cout<<"IMPOSSIBLE";
}
else
{
ll cur = n;
vector<ll> Ans;
while(Par[cur] != -1)
{
Ans.push_back(cur);
cur = Par[cur];
}
Ans.push_back(1);
cout<<Ans.size()<<endl;
reverse(Ans.begin(),Ans.end());
loop(i,0,Ans.size()-1)
{
cout<<Ans[i]<<" ";
}
}
#ifndef ONLINE_JUDGE
clock_t end= clock();
cout<<"\n\n\nExecuted In:"<< double(end-begin)/CLOCKS_PER_SEC*1000<<" ms";
#endif
return 0;
}