CSES - Shared codeLink to this code:
https://cses.fi/paste/ec6f372ba706d70b619ffe/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
typedef unsigned long long ull;
const int INF = 1e9;
const double EPS = 1e-9;
ll fast_ceiling(ll x, ll y) {
//(x + y - 1) / y;
if (x == 0LL) return 0LL;
return 1LL + ((x - 1LL) / y);
}
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
vector<int> sieve(int n) {
int sieve_size = n + 1;
bool sieve[sieve_size];
for (int i = 0; i < sieve_size; ++i) {
sieve[i] = true;
}
for (int i = 2; i * i <= n; ++i) {
if (sieve[i]) {
for (int j = i * i; j <= n; j += i) {
sieve[j] = false;
}
}
}
vector<int> ans;
for (int i = 2; i <= n; ++i) {
if (sieve[i]) {
ans.push_back(i);
}
}
return ans;
}
vector<ll> prime_factorization(ll n) {
ll factor = 2;
vector<ll> factors;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
while(n % i == 0) {
n /= i;
}
factors.push_back(i);
}
}
if (n > 1) {
factors.push_back(n);
}
return factors;
}
const ll MOD = 998244353LL;
ll mod_multiplication(ll a, ll b) {
return (((a * b) % MOD + MOD) % MOD);
}
ll mod_power(ll x, ll n) {
ll res = 1LL;
while(n > 0LL) {
if(n % 2LL) {
res = mod_multiplication(res, x);
}
x = mod_multiplication(x, x);
n >>= 1;
}
return res;
}
ll mod_inv(ll a) {
return mod_power(a, MOD - 2LL);
}
ll nCr(ll n, ll r) {
ll res = 1LL, div = 1LL;
for(ll i = 1 ; i <= r ; i++) {
res = mod_multiplication(res, n - i + 1LL);
div = mod_multiplication(div, i);
}
res = mod_multiplication(res, mod_inv(div));
return res;
}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
uint32_t next_pow2(uint32_t x) { return x == 1 ? 1 : 1 << (32 - __builtin_clz(x - 1)); }
uint32_t next_pow(uint32_t x) {return x == 1 ? 0 : 32 - __builtin_clz(x - 1); }
const ll A = 911382323LL;
const ll B = 972663749LL;
int n;
const int N = 2505;
map<int, int> edges[N];
bool visited[N];
int indeces[N];
unordered_set<int> unexplored;
bool dfs(int node, int parent, vector<int>& path, vector<ll>& pref_sum) {
unexplored.erase(node);
indeces[node] = path.size();
visited[node] = true;
path.push_back(node);
bool ok = false;
for (auto p : edges[node]) {
if (visited[p.first]) {
int pref_sum_start = indeces[p.first];
ll sum = pref_sum.back() + p.second - pref_sum[pref_sum_start];
if (sum < 0LL) {
path.push_back(p.first);
return true;
} else {
continue;
}
}
//if (visited[]) continue;
pref_sum.push_back(pref_sum.back() + p.second);
ok |= dfs(p.first, node, path, pref_sum);
pref_sum.pop_back();
if (ok) {
break;
}
}
visited[node] = false;
if (!ok) {
path.pop_back();
}
return ok;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
int m;
cin >> m;
rep(i, m) {
int u, v;
cin >> u >> v;
--u;
--v;
int c;
cin >> c;
if (edges[u].find(v) == edges[u].end()) {
edges[u].insert({v, c});
} else {
edges[u][v] = min(edges[u][v], c);
}
}
rep(i, n) unexplored.insert(i);
vector<ll> pref_sum(1, 0LL);
while (!unexplored.empty()) {
vector<int> path;
int node = *unexplored.begin();
bool ok = dfs(node, -1, path, pref_sum);
if (ok) {
int start;
for (int i = path.size() - 2; i >= 0; --i) {
start = i;
if (path[i] == path.back()) {
break;
}
}
cout << "YES\n";
for (int i = start; i < path.size(); ++i) {
cout << path[i] + 1 << ' ';
}
return 0;
}
}
cout << "NO\n";
return 0;
}