Advertisement
a53

ComponenteTC

a53
May 15th, 2022
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N = 100;
  5. vector<int> succesori[N+1], predecesori[N+1];
  6. vector<int>ctc[N+1];
  7. vector<int>s;
  8. bitset<N+1>viz;
  9. int n, nc, sol;
  10. void dfs(int x)
  11. {
  12. viz[x]=1;
  13. for(auto y: succesori[x])
  14. if (!viz[y])
  15. dfs(y);
  16. s.push_back(x);
  17. }
  18. void dfs_t(int x)
  19. {
  20. ctc[nc].push_back(x);
  21. viz[x] = 1;
  22. for(auto y: predecesori[x])
  23. if(!viz[y])
  24. dfs_t(y);
  25. }
  26. int main()
  27. {
  28. int m;
  29. cin>>n>>m;
  30. for(int i=0; i<m; i++)
  31. {
  32. int x, y;
  33. cin>>x>>y;
  34. succesori[x].push_back(y);
  35. predecesori[y].push_back(x);
  36. }
  37. for(int i=1; i<=n; i++)
  38. if(!viz[i])
  39. dfs(i);
  40. viz.reset();
  41. for(int i=(int)s.size()-1; i>=0; i--)
  42. if(!viz[s[i]])
  43. {
  44. nc++;
  45. dfs_t(s[i]);
  46. }
  47. for(int i=1; i<= nc; i++)
  48. if(ctc[i].size()==1)
  49. sol++;
  50. cout<<sol;
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement