Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. typedef vector<int> vi;
  8. vector<vi> List;
  9. vi visited;
  10. void dfs(int s){
  11. visited[s] = 1;
  12. for(int i = 0; i<List[s].size(); i++){
  13. if(visited[List[s][i]] == 0){
  14. dfs(List[s][i]);
  15. }
  16. }
  17. }
  18.  
  19. int main() {
  20.  
  21. int V,E,a,b;
  22.  
  23. cin>>V>>E;
  24.  
  25. for(int i = 1; i<=E; i++){
  26. cin>>a>>b;
  27. List[a].push_back(b);
  28. }
  29. List.assign(V+1, vi());
  30. visited.assign(V+1, 0);
  31. for(int i = 0; i<V; i++){
  32. dfs(i);
  33. cout << i << " ";
  34. for(int j=0; j<List[i].size(); j++){
  35. cout << List[i][j] << " ";
  36. }
  37. cout<<endl;
  38. }
  39.  
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement