Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <stack>
- using namespace std;
- ifstream cin("nuclee.in");
- ofstream cout("nuclee.out");
- using VI = vector<int>;
- using VVI = vector<VI>;
- using VB = vector<bool>;
- stack<int>stk;
- int n, m,c,N, k;
- VVI G;
- VI l, niv;
- VB v, inStack;
- void tarjan(int x)
- {
- l[x] = niv[x] = ++c;
- stk.push(x);
- inStack[x] = v[x] = true;
- for(const int&y : G[x])
- {
- if(!v[y])
- {
- tarjan(y);
- l[x] = min(l[x],l[y]);
- }
- else
- if(inStack[y])
- l[x] = min(l[x],niv[y]);
- }
- if(l[x] == niv[x])
- {
- N++;
- while (true)
- {
- int y = stk.top();
- stk.pop();
- inStack[y] = false;
- if (y == x)
- break;
- }
- }
- }
- int main()
- {
- cin >> n;
- G = VVI(n+1);
- v = inStack = VB(n+1);
- l = niv = VI(n+1);
- for(int i=1; i<=n; i++)
- {
- cin >> k;
- while(k--)
- {
- int x;
- cin >> x;
- G[i].push_back(x);
- }
- }
- for(int i=1; i<=n; i++)
- if(!v[i])
- tarjan(i);
- cout << N;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement