Advertisement
newb_ie

Untitled

Oct 16th, 2021
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. const int maxN = 10000;
  5. vector<int> adj[maxN];
  6. //~ adj[1] = {2,3}
  7. bool visited[maxN];
  8.  
  9. void dfs (int node) {
  10. cout << "ENTERING node " << node << '\n';
  11. visited[node] = true;
  12. for (int i = 0; i < (int) adj[node].size(); ++i) {
  13. int child = adj[node][i];
  14. if (visited[child] == false) {
  15. dfs(child);
  16. }
  17. }
  18. cout << "EXIT from " << node << '\n';
  19. }
  20.  
  21. int main () {
  22. ios::sync_with_stdio(false);
  23. cin.tie(nullptr);
  24. cout.tie(nullptr);
  25. int n, m;
  26. cin >> n >> m;
  27. for (int i = 1; i <= m; ++i) {
  28. int u,v; //u -> v and v -> u
  29. cin >> u >> v;
  30. adj[u].push_back(v);
  31. adj[v].push_back(u);
  32. }
  33. dfs(1);
  34. }
  35.  
  36. //~ 7 6
  37. //~ 1 2
  38. //~ 1 3
  39. //~ 2 4
  40. //~ 2 5
  41. //~ 3 6
  42. //~ 3 7
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement