Advertisement
STANAANDREY

pb2e 3/2/2021

Mar 2nd, 2021
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <algorithm>
  4. #include <limits>
  5. using namespace std;
  6. const int NMAX = 103;
  7. int n, m, k;
  8. list<int> edges[NMAX];
  9. bool canArrive[NMAX], vis[NMAX];
  10.  
  11. void read() {
  12.     cin >> n >> m >> k;
  13.     for (int i = 0; i < m; i++) {
  14.         int a, b;
  15.         cin >> a >> b;
  16.         edges[a].push_back(b);
  17.     }
  18. }
  19.  
  20. void dfs(int node) {
  21.     canArrive[node] = true;
  22.     for (int it : edges[node]) {
  23.         if (!canArrive[it]) {
  24.             dfs(it);
  25.         }
  26.     }
  27. }
  28.  
  29. int start;
  30. void dfs2(int node) {
  31.     vis[node] = true;
  32.     for (int it : edges[node]) {
  33.         if (vis[it] && it == start) {
  34.             cout << it << ' ';
  35.             return;
  36.         }
  37.         dfs2(it);
  38.     }
  39. }
  40.  
  41. int main() {
  42.     read();
  43.     dfs(k);
  44.     cout << "orase: ";
  45.     for (int i = 1; i <= n; i++) {
  46.         if (canArrive[i]) {
  47.             fill(vis, vis + n + 1, false);
  48.             start = i;
  49.             dfs2(i);
  50.         }
  51.     }
  52.     return 0;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement