Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAX = 500005;
- int n;
- vector<int> G[MAX], G_t[MAX];
- stack<int> S;
- int cor[MAX];
- void preenche(int v) {
- cor[v] = 1;
- for(int &w : G[v])
- if(!cor[w])
- preenche(w);
- S.push(v);
- }
- void dfs(int v) {
- cor[v] = 1;
- for(int &w : G_t[v])
- if(!cor[w])
- dfs(w);
- }
- bool kosaraju() {
- for(int i = 0; i < n; i++)
- if(!cor[i])
- preenche(i);
- memset(cor, 0, sizeof(cor));
- int sz = 0;
- while(!S.empty()) {
- int u = S.top();
- S.pop();
- if(cor[u]) continue;
- dfs(u);
- sz++;
- }
- return sz == 1;
- }
- int32_t main() {
- cin >> n;
- for(int i = 0; i < n; i++) {
- int a, b;
- cin >> a >> b; a--; b--;
- G[a].push_back(b);
- G_t[b].push_back(a);
- }
- if(kosaraju()) cout << "S\n";
- else cout << "N\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement