fork download
  1. /// Kosaraju by muoii
  2. /// vn.spoj.com/problems/TJALG/
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #define tag "spoj"
  6. #define maxn 0
  7. #define module 0
  8. #define oo 1000000007LL
  9. ///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  10. int ans;
  11. int n,m;
  12. vector< vector<int> > adj;
  13. vector< vector<int> > bak;
  14. ///tarjan data:
  15. int numbering;
  16. vector<bool> dd;
  17. vector<int> num,pos;
  18.  
  19. void topo(const int &u)
  20. {
  21. if(dd[u]) return;
  22. dd[u]=1;
  23.  
  24. for(const int &v: adj[u])
  25. topo(v);
  26.  
  27. pos[num[u]=numbering]=u;
  28. --numbering;
  29. }
  30. void trace(const int &u)
  31. {
  32. if(dd[u]) return;
  33. dd[u]=1;
  34.  
  35. for(const int &v: bak[u]) trace(v);
  36. }
  37. int main()
  38. {
  39. #ifdef dmdd
  40. freopen(tag".inp","r",stdin); freopen(tag".out","w",stdout);
  41. #endif // dmdd
  42. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  43.  
  44. cin>>n>>m;
  45.  
  46. ///load graph:
  47. adj.resize(n+1);bak.resize(n+1);dd.resize(n+1);num.resize(n+1);pos.resize(n+1);
  48. int x,y;
  49. while(m-->0) cin>>x>>y,adj[x].push_back(y),bak[y].push_back(x);
  50. ///update ans:
  51. numbering=n;
  52. fill(dd.begin(),dd.end(),0);
  53. for(int i=1;i<=n;i++)
  54. topo(i);
  55.  
  56. fill(dd.begin(),dd.end(),0);
  57. for(int i=1,u=pos[i];i<=n;u=pos[++i])
  58. ans+=!dd[u],trace(u);
  59.  
  60. cout<<ans;
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty