alexOloieri

InfoGIM 17.04.2021 BFS

Apr 17th, 2021
637
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fstream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #define NMAX 101
  6. using namespace std;
  7. ifstream fin("BFS.in");
  8. ofstream fout("BFS.out");
  9.  
  10. int n, m, start;
  11. int x, y;
  12. vector<int> G[NMAX];
  13. bool vizitat[NMAX];
  14.  
  15. void bfs(int k) {
  16.     queue<int> Q;
  17.     Q.push(k);
  18.     vizitat[k] = true;
  19.  
  20.     while (!Q.empty()) {
  21.         int nodCurent = Q.front();
  22.         fout << nodCurent << ' ';
  23.         Q.pop();
  24.         int sz = G[nodCurent].size();
  25.         for (int i=0;i<sz;++i) {
  26.             int vecin = G[nodCurent][i];
  27.             if (!vizitat[vecin]) {
  28.                 Q.push(vecin);
  29.                 vizitat[vecin] = true;
  30.             }
  31.         }
  32.     }
  33. }
  34.  
  35. int main()
  36. {
  37.     fin >> n >> m >> start;
  38.     for (int i=1;i<=m;++i) {
  39.         // Citesc muchiile
  40.         fin >> x >> y;
  41.         G[x].push_back(y);
  42.         G[y].push_back(x);
  43.     }
  44.     // Sortez listele de adiacenta ca sa respecte conditia de parcurgere din enunt
  45.     for (int i=1;i<=n;++i) {
  46.         sort(G[i].begin(), G[i].end());
  47.     }
  48.     bfs(start);
  49.     fin.close();
  50.     fout.close();
  51.     return 0;
  52. }
  53.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×