Link to this code: https://cses.fi/paste/e8fb5cf71cd11ea62f0f02/
#include<bits/stdc++.h>
//#define Woody 089487
#define int long long 
#define lowbit(x) (x&-x)
#define max3(a,b,c) max(max(a,b),c)
#define repi(l,r) for(int i=l;i<=r;i++)
#define repj(l,r) for(int j=l;j<=r;j++)
#define mp make_pair
#define eb emplace_back
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define all(v) v.begin(),v.end()
#define SETIO(s) ifstream cin(s+".in");ofstream cout(s+".out");
#ifdef Woody
#define quick ios::sync_with_stdio(0);cin.tie(0);
#else
#define quick
#endif
#define INF 1e18
using namespace std;
typedef pair<int,int> pii;
const int N=2500+7;
const int M=5007;
struct edge{
	int a;
	int b;
	int c;	
};
int d[N];
int p[N];
edge e[M];
signed main(){
	quick
	int n,m;
	cin>>n>>m;
	int x;
	fill(d,d+N,INF);
	for(int i=1;i<=n;i++) p[i]=i;
	for(int i=0;i<m;i++) cin>>e[i].a>>e[i].b>>e[i].c;
	for(int i=0;i<n;i++){
		x=-1;
		for(int j=0;j<m;j++){
			if(d[e[j].b]>d[e[j].a]+e[j].c){
				d[e[j].b]=d[e[j].a]+e[j].c;
				p[e[j].b]=e[j].a;
				x=e[j].b;
			}
		}
	}
	if(x==-1){
		cout<<"NO\n";
	}
	else{
		int y=x;
		for(int i=0;i<n;i++){
			y=p[y];
		}
		vector<int> path;
		for(int cur=y;;cur=p[cur]){
			path.eb(cur);
			if(cur==y&&SZ(path)>1)  break;
		}
		reverse(all(path));
		cout<<"YES\n";
		for(int i:path) cout<<i<<" ";cout<<"\n";
	}
}