-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSES_Cycle_Finding.cpp
More file actions
74 lines (60 loc) · 1.25 KB
/
Copy pathCSES_Cycle_Finding.cpp
File metadata and controls
74 lines (60 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const ll inf = 1e18;
struct Edge{
ll a;
ll b;
ll w;
};
vector<Edge> edges;
void solve(){
ll n, m;
cin>>n>>m;
for(ll i=0; i<m; i++){
Edge e;
cin>>e.a>>e.b>>e.w;
edges.push_back(e);
}
vector<ll> d(n+1, 0);
vector<ll> p(n+1, -1);
ll changed = -1;
for(ll i=0; i<n; i++){
changed = -1;
for(Edge e: edges){
if(d[e.b] > d[e.a]+e.w){
d[e.b] = max(-inf, d[e.a]+e.w);
p[e.b] = e.a;
changed = e.b;
}
}
}
if(changed == -1){
cout<<"NO"<<endl;
return;
}
ll y = changed;
for(ll i=0; i<n; i++){
y = p[y];
}
vector<ll> path;
for(ll i = y; ; i = p[i]){
path.push_back(i);
if(path.size() > 1 and i == y) break;
}
reverse(path.begin(), path.end());
cout<<"YES"<<endl;
for(auto it: path){
cout<<it<<" ";
}
cout<<endl;
}
signed main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
optimize();
solve();
return 0;
}