Advertisement
MathQ_

Untitled

Jan 17th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. typedef long long ll;
  5.  
  6. #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  7.  
  8. using namespace std;
  9.  
  10. const ll INF = 1e5;
  11. int mx[101][101];
  12. vector<vector<int>> g;
  13. vector<int> cycle, cnt, ans;
  14.  
  15. void dfs(int v) {
  16.     cnt[v]++;
  17.     cycle.push_back(v + 1);
  18.     if (cnt[v] >= 2) {
  19.         int j = 0;
  20.         while (cycle[j] != v + 1) ++j;
  21.         for (int i = j; i < cycle.size(); ++i) {
  22.             ans.push_back(cycle[i]);
  23.         }
  24.         return;
  25.     }
  26.     for (auto u : g[v]) {
  27.         if (mx[u][u] < 0) {
  28.             dfs(u);
  29.             return;
  30.         }
  31.     }
  32. }
  33.  
  34. int main() {
  35.     fast
  36.    
  37.     int n;
  38.     cin >> n;
  39.     g.resize(n); cnt.resize(n, 0);
  40.     for (int i = 0; i < n; ++i) {
  41.         for (int j = 0; j < n; ++j) {
  42.             cin >> mx[i][j];
  43.             if (mx[i][j] != INF) {
  44.                 g[i].push_back(j);
  45.             }
  46.         }
  47.     }
  48.  
  49.     for (int k = 0; k < n; ++k) {
  50.         for (int i = 0; i < n; ++i) {
  51.             for (int j = 0; j < n; ++j) {
  52.                 if (mx[i][k] < INF && mx[k][j] < INF) {
  53.                     mx[i][j] = min(mx[i][j], mx[i][k] + mx[k][j]);
  54.                 }
  55.             }
  56.         }
  57.     }
  58.     for (int i = 0; i < n; ++i) {
  59.         if (mx[i][i] < 0) {
  60.             dfs(i);
  61.             break;
  62.         }
  63.     }
  64.     if (cycle.empty()) {
  65.         cout << "NO" << '\n';
  66.     } else {
  67.         cout << "YES" << '\n';
  68.         cout << ans.size() << '\n';
  69.         for (auto el : ans) cout << el << " ";
  70.     }
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement