Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <set>
  5. #include <vector>
  6. #include <iomanip>
  7. #include <cstdio>
  8. using namespace std;
  9.  
  10. typedef long double ld;
  11.  
  12. const int N = 1e5;
  13.  
  14. vector <bool> used;
  15. vector <int> v[N];
  16.  
  17. void dfs(int x)
  18. {
  19. used[x] = true;
  20. for (int i = 0; i < v[x].size(); i++)
  21. {
  22. if (!used[v[x][i]])
  23. dfs(v[x][i]);
  24. }
  25. }
  26.  
  27.  
  28. int main() {
  29. ios::sync_with_stdio(0);
  30. int n, m, a, b;
  31. cin >> n >> m;
  32.  
  33. for (int i = 0; i < m; i++)
  34. {
  35. cin >> a >> b;
  36. a--; b--;
  37. v[a].push_back(b);
  38. }
  39.  
  40. for (int i = 0; i < n; i++) {
  41. if (!used[i])
  42. dfs(i);
  43. }
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement