Advertisement
STANAANDREY

bfs

Jan 9th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define MAXPEEKSNR 100
  5. int peeks, links;
  6. vector<int> graph[MAXPEEKSNR];
  7.  
  8. int bfs(int startNode)
  9. {
  10.     queue<int> bfsQueue;
  11.     vector<bool> visited(peeks + 1, false);
  12.     unsigned visnr = 0;
  13.  
  14.     bfsQueue.push(startNode);
  15.     visited[startNode] = true;
  16.  
  17.     while (!bfsQueue.empty())
  18.     {
  19.         int currNode = bfsQueue.front();
  20.         bfsQueue.pop();
  21.         for (int i = 0; i < (int)graph[currNode].size(); i++)
  22.         {
  23.             if (!visited[graph[currNode][i]])
  24.             {
  25.                 visited[graph[currNode][i]] = true;
  26.                 bfsQueue.push(graph[currNode][i]);
  27.             }
  28.         }
  29.         //cerr << currNode << ' ';
  30.         visnr++;
  31.     }
  32.     return visnr;
  33. }
  34.  
  35. int main()
  36. {
  37.     cin >> peeks >> links;
  38.     int node;
  39.     for (int i = 0; i < links; i++)
  40.     {
  41.         int x, y;
  42.         cin >> x >> y;
  43.         graph[x].push_back(y);
  44.         graph[y].push_back(x);
  45.         node = x;
  46.     }
  47.  
  48.     int visnr = bfs(node);
  49.     cout << endl << visnr;
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement