Advertisement
nq1s788

dfs

Jan 26th, 2025
70
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 <string>
  3. #include "Animal.cpp"
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. vector<bool> used;
  10. vector<vector<int>> g;
  11.  
  12. void dfs(int from) {
  13.     used[from] = true;
  14.     for (auto to : g[from]) {
  15.         if (!used[to]) dfs(to);
  16.     }
  17. }
  18.  
  19. int main() {
  20.     int n, m; //n -- кол-во вершин, m -- кол-во ребер
  21.     cin >> n >> m;
  22.     g.resize(n);
  23.     for (int i = 0; i < m; i++) {
  24.         int x, y;
  25.         cin >> x >> y;
  26.         x--, y--;
  27.         g[x].push_back(y);
  28.         g[y].push_back(x);
  29.     }
  30.     used.assign(n, false);
  31.     dfs(0);
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement