fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define SIZE 10000
  5. vector<int> graph[SIZE];
  6. bool visited[SIZE] = {0};
  7.  
  8. // For tree visited array is not required
  9. void dfsForTree(int curr, int parent) {
  10. printf("%d ", curr);
  11.  
  12. for(int i = 0;i < graph[curr].size();i++) {
  13. int adj = graph[curr][i];
  14. if(adj != parent)
  15. dfsForTree(adj, curr);
  16. }
  17. }
  18.  
  19. void dfs(int curr) {
  20. printf("%d ", curr);
  21. visited[curr] = true;
  22.  
  23. for(int adj: graph[curr]) {
  24. if(!visited[adj]) {
  25. dfs(adj);
  26. }
  27. }
  28. }
  29.  
  30. int main() {
  31. int root;
  32.  
  33. // Graph construction assumed as done
  34. dfsForTree(root, -1);
  35. dfs(root);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 4428KB
stdin
Standard input is empty
stdout
0 0