Advertisement
davidalexandru13

conexidad

Jun 6th, 2020
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <climits>
  6. #include <set>
  7. #include <queue>
  8. using namespace std;
  9. fstream f("text.txt");
  10. ofstream o("branza.out");
  11. int n, e;
  12. vector<int>g[105];
  13. int a, b;
  14. bool v[105];
  15. int componente;
  16. vector<pair<int, int>>graf[105];
  17. int d;
  18.  
  19.  
  20. void bfs(int cv, int data)
  21. {
  22.     queue<int>coada;
  23.     vector<bool>w(105, false);
  24.     coada.push(cv);
  25.     int muchii = 1;
  26.     graf[data].push_back({ cv,muchii });
  27.     v[cv] = true;
  28.     w[cv] = true;
  29.     while (!coada.empty())
  30.     {
  31.         int nod = coada.front();
  32.         coada.pop();
  33.         for (size_t i = 0; i < g[nod].size(); i++)
  34.         {
  35.             int vecin = g[nod][i];
  36.             if (w[vecin] == false)
  37.             {
  38.                 coada.push(vecin);
  39.                 w[vecin] = true;
  40.                 v[vecin] = true;
  41.                 graf[data].push_back({ vecin,muchii++ });
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48.  
  49.  
  50. int main()
  51. {
  52.     f >> n >> e;
  53.  
  54.     for (size_t i = 1; i <= n; i++)
  55.     {
  56.         f >> a >> b;
  57.         g[a].push_back(b);
  58.         g[b].push_back(a);
  59.     }
  60.  
  61.     for (size_t i = 1; i <= n; i++)
  62.     {
  63.         if (!v[i])
  64.         {
  65.             bfs(i, d);
  66.             d++;
  67.         }
  68.     }
  69.  
  70.     for (size_t i = 0; i < d; i++)
  71.     {
  72.         for (size_t j = 0; j < graf[i].size(); j++)
  73.         {
  74.             cout << graf[i][j].first << " ";
  75.         }
  76.         cout << "\n";
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement