alexOloieri

InfoGIM 17.04.2021 DFS

Apr 17th, 2021
566
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fstream>
  2. #include <vector>
  3. #include <algorithm>
  4. #define NMAX 101
  5. using namespace std;
  6. ifstream fin("dfs.in");
  7. ofstream fout("dfs.out");
  8.  
  9. int n, m, start;
  10. int x, y;
  11. vector<int> G[NMAX];
  12. bool vizitat[NMAX];
  13.  
  14. void dfs(int k) {
  15.     // Vizitare nod
  16.     fout << k << ' ';
  17.     vizitat[k] = true;
  18.     // Continuare parcurgere
  19.     int sz = G[k].size();
  20.     for (int i=0;i<sz;++i) {
  21.         int vecin = G[k][i];
  22.         if (vizitat[vecin] == false) { //  Ma intereseaza vecinii care n-au fost vizitati
  23.             dfs(vecin);
  24.         }
  25.     }
  26. }
  27.  
  28. int main()
  29. {
  30.     fin >> n >> m >> start;
  31.     for (int i=1;i<=m;++i) {
  32.         // Citesc muchiile
  33.         fin >> x >> y;
  34.         G[x].push_back(y);
  35.         G[y].push_back(x);
  36.     }
  37.     // Sortez listele de adiacenta ca sa respecte conditia de parcurgere din enunt
  38.     for (int i=1;i<=n;++i) {
  39.         sort(G[i].begin(), G[i].end());
  40.     }
  41.     dfs(start);
  42.     fin.close();
  43.     fout.close();
  44.     return 0;
  45. }
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.

×