Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <stack>
- #include <cstring>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main() {
- int n;
- cin >> n;
- vector<int> adj[n];
- for(int i = 0; i < n; i++) {
- int a, b;
- cin >> a >> b;
- a--, b--;
- adj[a].push_back(b);
- }
- bool consegue = true;
- for(int i = 0; i < n; i++) {
- int vis[n];
- memset(vis, 0, sizeof vis);
- stack<int> pilha;
- pilha.push(i);
- while(!pilha.empty()) {
- int v = pilha.top();
- vis[v] = 1;
- bool found = false;
- for(int i = 0; i < n; i++)
- if(find(adj[v].begin(), adj[v].end(), i) != adj[v].end() && v != i && vis[i] == 0) {
- pilha.push(i);
- vis[i] = 1;
- found = true;
- }
- if(!found)
- pilha.pop();
- }
- for(int j = 0; j < n; j++)
- if(!vis[j]) {
- i = n + 1;
- consegue = false;
- break;
- }
- }
- cout << ((consegue) ? "S" : "N") << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement