Advertisement
matheus_monteiro

Rodovia

Jun 9th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAX = 500005;
  4.  
  5. int n;
  6. vector<int> G[MAX], G_t[MAX];
  7. stack<int> S;
  8. int cor[MAX];
  9.  
  10. void preenche(int v) {
  11.     cor[v] = 1;
  12.     for(int &w : G[v])
  13.         if(!cor[w])
  14.             preenche(w);
  15.     S.push(v);
  16. }
  17.  
  18. void dfs(int v) {
  19.     cor[v] = 1;
  20.     for(int &w : G_t[v])
  21.         if(!cor[w])
  22.             dfs(w);
  23. }
  24.  
  25. bool kosaraju() {
  26.     for(int i = 0; i < n; i++)
  27.         if(!cor[i])
  28.             preenche(i);
  29.     memset(cor, 0, sizeof(cor));
  30.     int sz = 0;
  31.     while(!S.empty()) {
  32.         int u = S.top();
  33.         S.pop();
  34.         if(cor[u]) continue;
  35.         dfs(u);
  36.         sz++;
  37.     }
  38.     return sz == 1;
  39. }
  40.  
  41. int32_t main() {
  42.     cin >> n;
  43.  
  44.     for(int i = 0; i < n; i++) {
  45.         int a, b;
  46.         cin >> a >> b; a--; b--;
  47.         G[a].push_back(b);
  48.         G_t[b].push_back(a);
  49.     }
  50.     if(kosaraju()) cout << "S\n";
  51.     else cout << "N\n";
  52.  
  53.     return 0;
  54. }
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement