Advertisement
Josif_tepe

Untitled

Oct 5th, 2023
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <queue>
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <set>
  7. #include <cstring>
  8. #include <stack>
  9. #include <algorithm>
  10. #include <map>
  11. #include <cmath>
  12. //#include <bits/stdc++.h>
  13. using namespace std;
  14. typedef long long ll;
  15. const int maxn = 1e5 + 10;
  16. const ll INF = 3e16 + 10;
  17. const int max_point = 10000000;
  18. vector<int> graph[maxn];
  19.  
  20. void dfs(int node, int parent) {
  21.     cout << node + 1 << endl;
  22.     for(int i = 0; i < (int) graph[node].size(); i++) {
  23.         int neighbour = graph[node][i];
  24.         if(neighbour != parent) {
  25.             dfs(neighbour, node);
  26.         }
  27.     }
  28. }
  29. int main() {
  30.     ios_base::sync_with_stdio(false);
  31.     int n;
  32.     cin >> n;
  33.    
  34.     for(int i = 0; i < n - 1; i++) {
  35.         int a, b;
  36.         cin >> a >> b;
  37.         graph[a].push_back(b);
  38.         graph[b].push_back(a);
  39.        
  40.     }
  41.     dfs(0, -1);
  42.     return 0;
  43. }
  44. /*
  45.  1: 10, 40
  46.  3: 20
  47.  
  48.  
  49.  **/
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement