Advertisement
coloriot

HA32_Cycle(11)

Nov 24th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. using namespace std;
  5.  
  6. class Graph {
  7. private:
  8.     int N, M;
  9.     vector<vector<int>> adj;
  10.     vector<int> visited; // 0: не посещенный, 1: посещенный, 2: посещенный
  11.     vector<int> parent;
  12.     bool has_cycle;
  13.  
  14. public:
  15.     Graph(int n, int m) : N(n), M(m) {
  16.         adj.resize(N + 1);
  17.         visited.resize(N + 1, 0);
  18.         parent.resize(N + 1, -1);
  19.         has_cycle = false;
  20.     }
  21.  
  22.     void addEdge(int u, int v) {
  23.         adj[u].push_back(v);
  24.     }
  25.  
  26.     void detectCycle() {
  27.         stack<int> dfs_stack;
  28.  
  29.         for (int u = 1; u <= N && !has_cycle; ++u) {
  30.             if (visited[u] == 0) {
  31.                 dfs_stack.push(u);
  32.                 visited[u] = 1;
  33.                 parent[u] = -1;
  34.  
  35.                 while (!dfs_stack.empty()) {
  36.                     int top = dfs_stack.top();
  37.                     bool allNeighborsVisited = true;
  38.  
  39.                     for (int v : adj[top]) {
  40.                         if (visited[v] == 0) {
  41.                             visited[v] = 1;
  42.                             parent[v] = top;
  43.                             dfs_stack.push(v);
  44.                             allNeighborsVisited = false;
  45.                             break;
  46.                         } else if (visited[v] == 1) {
  47.                             stack<int> cycle_stack;
  48.                             cycle_stack.push(v);
  49.                             int temp = top;
  50.                             while (temp != v) {
  51.                                 cycle_stack.push(temp);
  52.                                 temp = parent[temp];
  53.                             }
  54.                             cout << "YES" << endl;
  55.                             while (!cycle_stack.empty()) {
  56.                                 cout << cycle_stack.top() << " ";
  57.                                 cycle_stack.pop();
  58.                             }
  59.                             cout << endl;
  60.                             has_cycle = true;
  61.                             return;
  62.                         }
  63.                     }
  64.  
  65.                     if (allNeighborsVisited) {
  66.                         dfs_stack.pop();
  67.                         visited[top] = 2;
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     void findCycle() {
  75.         detectCycle();
  76.     }
  77.  
  78.     void printCycle() {
  79.         if (!has_cycle) {
  80.             cout << "NO" << endl;
  81.         }
  82.     }
  83. };
  84.  
  85. int main() {
  86.     ios::sync_with_stdio(false);
  87.     cin.tie(NULL);
  88.     int N, M;
  89.     cin >> N >> M;
  90.     Graph graph(N, M);
  91.     for (int i = 0; i < M; ++i) {
  92.         int u, v;
  93.         cin >> u >> v;
  94.         graph.addEdge(u, v);
  95.     }
  96.     graph.findCycle();
  97.     graph.printCycle();
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement