Advertisement
nq1s788

dfs

Mar 8th, 2025
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <deque>
  5.  
  6. using namespace std;
  7.  
  8. vector <vector<int>> g;
  9. vector<bool> used;
  10.  
  11. void dfs(int h) {
  12.     used[h] = true;
  13.     for (auto e : g[h]) {
  14.         if (!used[e]) dfs(e);
  15.     }
  16. }
  17.  
  18. int main() {
  19.     int n, m;
  20.     cin >> n >> m;
  21.     g.resize(n);
  22.     int x, y;
  23.     for (int i = 0;i < m;i++)
  24.     {
  25.         cin >> x >> y;
  26.         g[x - 1].push_back(y - 1);
  27.         g[y - 1].push_back(x - 1);
  28.     }
  29.     int start;
  30.     cin >> start;
  31.     used.assign(n, false);
  32.     dfs(start - 1);
  33.     for (auto e : used) cout << (int)e << ' ';
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement