Advertisement
rdsedmundo

rod_temp.cpp

May 17th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <stack>
  4. #include <cstring>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int main() {
  11.  
  12.     int n;
  13.     cin >> n;
  14.  
  15.     vector<int> adj[n];
  16.  
  17.     for(int i = 0; i < n; i++) {
  18.         int a, b;
  19.         cin >> a >> b;
  20.  
  21.         a--, b--;
  22.  
  23.         adj[a].push_back(b);
  24.     }
  25.  
  26.     bool consegue = true;
  27.  
  28.     for(int i = 0; i < n; i++) {
  29.         int vis[n];
  30.         memset(vis, 0, sizeof vis);
  31.  
  32.         stack<int> pilha;
  33.         pilha.push(i);
  34.  
  35.         while(!pilha.empty()) {
  36.             int v = pilha.top();
  37.             vis[v] = 1;
  38.  
  39.             bool found = false;
  40.  
  41.             for(int i = 0; i < n; i++)
  42.                 if(find(adj[v].begin(), adj[v].end(), i) != adj[v].end() && v != i && vis[i] == 0) {
  43.                     pilha.push(i);
  44.                     vis[i] = 1;
  45.                     found = true;
  46.                 }
  47.  
  48.             if(!found)
  49.                 pilha.pop();
  50.         }
  51.  
  52.         for(int j = 0; j < n; j++)
  53.             if(!vis[j]) {
  54.                 i = n + 1;
  55.                 consegue = false;
  56.                 break;
  57.             }
  58.     }
  59.  
  60.     cout << ((consegue) ? "S" : "N") << endl;
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement