Advertisement
nq1s788

Untitled

Mar 22nd, 2025
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <deque>
  5.  
  6. #define se second
  7. #define fi first
  8. #define mp make_pair
  9. #define pb push_back
  10.  
  11. using namespace std;
  12.  
  13. int n, m;
  14. vector<vector<int>> g;
  15. vector<bool> used;
  16. bool cycle;
  17.  
  18. void dfs(int h) {
  19.     used[h] = true;
  20.     for (auto e : g[h]) {
  21.         if (used[e]) {
  22.             cycle = true;
  23.             return;
  24.         }
  25.         if (!used[e]) dfs(e);
  26.     }
  27. }
  28.  
  29. int main() {
  30.     cin >> n >> m;
  31.     vector<bool> is_root(n, true);
  32.     for (int i = 0; i < m; i++) {
  33.         int x, y;
  34.         cin >> x >> y;
  35.         x--, y--;
  36.         g[x].pb(y);
  37.         is_root[y] = false;
  38.     }
  39.     used.assign(n, false);
  40.     cycle = false;
  41.     for (int i = 0; i < n; i++) {
  42.         if (!used[i]) dfs(i);
  43.     }
  44.     if (cycle) {
  45.         cout << "No";
  46.         return 0;
  47.     }
  48.     for (int i = 0; i < n; i++) {
  49.         if (is_root[i])
  50.     }
  51.     return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement