Hezov

Biconexitate

Jul 16th, 2025
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. // https://www.pbinfo.ro/probleme/1572/componentebiconexe
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <array>
  6. using namespace std;
  7. ifstream cin("componentebiconexe.in");
  8. ofstream cout("componentebiconexe.out");
  9. #define RADACINA 1
  10. const int mxN = 1e5;
  11. vector<int> adj[mxN+10],order,compBi[mxN+10], pA; // pA -> puncte de articulatie
  12. vector<array<int,2>> mC; // mC -> muchii critice
  13. int depth[mxN+10], minD[mxN+10]; // minD -> nivel minim accesibil
  14. int visited[mxN+10], nrCompBi;
  15. void DFS(int node, int parent)
  16. {
  17.     order.push_back(node); // order -> ordinea in care sunt parcurse nodurile in DFS
  18.     visited[node] = true;
  19.     depth[node] = depth[parent] + 1;
  20.     minD[node] = depth[node];
  21.     int cntVis = 0;
  22.     bool isCrit = false; // daca este punct de articulatie / nod critic
  23.     for(auto it : adj[node])
  24.     {
  25.         if(it == parent) continue;
  26.         if(visited[it])
  27.             minD[node] = min(minD[node], depth[it]);
  28.         else
  29.         {
  30.             cntVis++;
  31.             DFS(it,node);
  32.             minD[node] = min(minD[node],minD[it]);
  33.             if(depth[node] <= minD[it]) isCrit = true;
  34.             if(depth[node] < minD[it])
  35.                 mC.push_back({node,it});
  36.             if(depth [node] <= minD[it]) // componenta biconexa
  37.             {
  38.                 nrCompBi++;
  39.                 while(order.back() != it)
  40.                     compBi[nrCompBi].push_back(order.back()), order.pop_back();
  41.                 // fac parte din componenta si (node) si (it) care inca nu sunt adaugate.
  42.                 compBi[nrCompBi].push_back(node);
  43.                 compBi[nrCompBi].push_back(it);
  44.                 order.pop_back(); // era it acum.
  45.             }
  46.  
  47.         }
  48.     }
  49.     if(node == RADACINA && cntVis < 2)
  50.         isCrit = false;
  51.     if(isCrit)
  52.         pA.push_back(node);
  53. }
  54. int main()
  55. {
  56.     int cerinta;
  57.     cin >> cerinta;
  58.     int n , m ;
  59.     cin >> n >> m;
  60.     for(int i = 1;i<=m;i++)
  61.     {
  62.         int a, b;
  63.         cin >> a >> b;
  64.         adj[a].push_back(b);
  65.         adj[b].push_back(a);
  66.     }
  67.     DFS(1,0);
  68.     if(cerinta == 1) // sa se afiseze componentele biconexe
  69.     {
  70.         cout << nrCompBi << '\n';
  71.         for(int i = 1;i<=nrCompBi;i++)
  72.         {
  73.             cout << compBi[i].size() << ' '; // nr de elem din componenta
  74.             for(auto it : compBi[i]) // afisam elementele din componenta
  75.                 cout << it << ' ';
  76.             cout << '\n';
  77.         }
  78.     }
  79.     if(cerinta == 2) // sa se afiseze punctele  de articulatie  (punctele critice)
  80.     {
  81.         cout << pA.size() << '\n';
  82.         for(auto it : pA)
  83.             cout << it << ' ';
  84.         cout << '\n';
  85.     }
  86.     if(cerinta == 3) // sa se afiseze puntile (muchiile) critice
  87.     {
  88.         cout << mC.size() << '\n';
  89.         for(auto it : mC)
  90.             cout << it[0] << ' ' << it[1] << '\n';
  91.     }
  92.     return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment