[text] xx

Viewer

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5 + 10;
  4.  
  5. int level[N];
  6. int vis[N];
  7. vector<int> g[N];
  8. void bfs(int source)
  9. {
  10.     queue<int> q;
  11.     q.push(source);
  12.     vis[source] = 1;
  13.  
  14.     while (!q.empty())
  15.     {
  16.         int Current_vertex = q.front();
  17.         q.pop();
  18.  
  19.         cout << Current_vertex<<"\t";
  20.  
  21.         for (int i = 0; i < g[Current_vertex].size(); ++i)
  22.         {
  23.             int child = g[Current_vertex][i];
  24.  
  25.             if (!vis[child])
  26.             {
  27.                 q.push(child);
  28.                 vis[child] = 1;
  29.             }
  30.         }
  31.         //cout << endl;
  32.     }
  33. }
  34.  
  35. int main()
  36. {
  37.     int n;
  38.     cin >> n;
  39.     while (n--)
  40.     {
  41.         int x, y;
  42.         cin >> x >> y;
  43.         g[x].push_back(y);
  44.         g[y].push_back(x);
  45.     }
  46.     bfs(1);
  47. }

Editor

You can edit this paste and save as new:


File Description
  • xx
  • Paste Code
  • 30 Apr-2024
  • 823 Bytes
You can Share it: