Advertisement
Josif_tepe

Untitled

Mar 3rd, 2022
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int n, m;
  5. vector<int> graph[105];
  6. bool visited[105];
  7.  
  8. void dfs(int node) {
  9.     visited[node] = true;
  10.     cout << node << endl;
  11.     for(int i = 0; i < graph[node].size(); i++) {
  12.         int sosed = graph[node][i];
  13.         if(!visited[sosed]) {
  14.             dfs(sosed);
  15.         }
  16.     }
  17. }
  18. int main()
  19. {
  20.     cin >> n >> m;
  21.     for(int i = 0; i < n; i++) {
  22.         visited[i] = false;
  23.     }
  24.     for(int i = 0; i < m; i++) {
  25.         int a, b;
  26.         cin >> a >> b;
  27.         graph[a].push_back(b);
  28.         graph[b].push_back(a);
  29.     }
  30.     dfs(0);
  31.    return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement