Advertisement
Guest User

Get a tree

a guest
Apr 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. int n, m, cnt = 0, a, b, v, arr[101][101], lamps[101];
  10.  
  11. void dfs(int v)
  12. {
  13.     lamps[v] = 1;
  14. //  cout << v + 1 << " ";
  15.    
  16.     for (int i = 0; i < n; i++)
  17.     {
  18.         if(arr[v][i] == 1 && lamps[i] == 0)
  19.         {
  20.             cout << v + 1 << " " << i + 1 << "\n";
  21.             dfs(i);
  22.         }
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.     //--------------------------------
  29.     ios_base::sync_with_stdio(false);
  30.     cin.tie(0);
  31.     cout.tie(0);
  32.     //--------------------------------
  33.    
  34.     cin >> n >> m;
  35.    
  36.     for (int i = 0; i < m; i++)
  37.     {
  38.         cin >> a >> b;
  39.         arr[a-1][b-1] = arr[b-1][a-1] = 1;
  40.     }
  41.    
  42.     dfs(0);
  43.    
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement