Advertisement
tepyotin2

Cycle Finding

May 21st, 2025
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. struct Edge{
  8.     int from, to;
  9.     ll weight;
  10. };
  11.  
  12. int t;
  13. int n, m;
  14. vector<Edge> graph;
  15. int parent[5001];
  16. bool visited[5001];
  17.  
  18. void print(int v){
  19.     //cout << "HI" << '\n';
  20.     while(!visited[v]){
  21.         //#1. Find first value to actually begin cycle
  22.         visited[v] = true;
  23.         v = parent[v];
  24.     }
  25.     vector<int> ans;
  26.     int temp = parent[v];
  27.     ans.push_back(v);
  28.     //cout << " v: " << v << '\n';
  29.     //int count = 1;
  30.     while(temp != v){
  31.         //count++;
  32.         //if(count>20) break;
  33.         //cout << "temp: " << temp << '\n';
  34.         ans.push_back(temp);
  35.         temp = parent[temp];
  36.     }
  37.     ans.push_back(v);
  38.     reverse(ans.begin(), ans.end());
  39.     for(auto a: ans){
  40.         cout << " " << a;
  41.     }
  42.     cout << '\n';
  43.     //cout << ans[0] << '\n';
  44. }
  45.  
  46. bool isNegative(){
  47.     vector<ll> dist;
  48.     dist.resize(n+1, 0);
  49.     dist[1] = 0;
  50.     bool check = false;
  51.     for(int i=1; i<=n; i++){
  52.         //cout << "i: " << i << '\n';
  53.         check = false;
  54.         for(Edge e: graph){
  55.             //if(dist[e.from] != INT_MAX){
  56.             //cout << "e.to: " << e.to << ", e.from: " << e.from << ", e.weight: " << e.weight << '\n';
  57.             if(dist[e.to]>dist[e.from]+e.weight){
  58.                 dist[e.to] = dist[e.from]+e.weight;
  59.                 //cout << "i: " << i << ", dist[e.to]: " << dist[e.to] << ", dist[e.from]: " << dist[e.from] << '\n';
  60.                 parent[e.to] = e.from;
  61.                 check = true;
  62.                 if(i==n){
  63.                     cout << "YES";
  64.                     print(e.to);
  65.                     //#1. need to use value that is actually part of cycle or else infinite loop on some cases
  66.                     return true;
  67.                 }
  68.                 //cout << "HI" << '\n';
  69.             }
  70.             //}
  71.         }
  72.         if(!check) break;
  73.     }
  74.     return false;
  75. }
  76.  
  77. int main(){
  78.     //freopen("cyclefind.in", "r", stdin);
  79.    
  80.     cin >> n >> m;
  81.     graph.clear();
  82.     graph.resize(m);
  83.     //dist.resize(n+1, INT_MAX);
  84.     for(int i=0; i<m; i++){
  85.         cin >> graph[i].from >> graph[i].to >> graph[i].weight;
  86.         //cout << graph[i].from << ", " << graph[i].to << ", " << graph[i].weight << '\n';
  87.     }
  88.     if(isNegative()){
  89.         //cout << "YES" << '\n';
  90.     }else{
  91.         cout << "NO" << '\n';
  92.     }
  93.    
  94.     return 0;
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement