Cod sursa(job #2806613)

Utilizator victorzarzuZarzu Victor victorzarzu Data 22 noiembrie 2021 20:39:34
Problema Componente biconexe Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>
#define oo 0x3f3f3f3f 
#define greutate first
#define profit second

using namespace std;
ifstream f("biconex.in");
ofstream g("biconex.out");
int n, m, nr;
vector<int> graf[100001];
vector<int> result[10001];
bool viz[100001];
stack<int> s;
int dfn[100001], low[100001];  //low[x] => the lowest index of dfs indexes that can be reached from x through another path than the dfs one

void read()
{
  f>>n>>m;
  int x, y;
  for(int i = 1;i <= m;++i)
    f>>x>>y, graf[x].push_back(y), graf[y].push_back(x);    
}

void dfs(int node, int index)
{
  viz[node] = true;
  dfn[node] = low[node] = index;
  s.push(node);
  for(auto it : graf[node])
    if(viz[it])
      low[node] = min(dfn[it], low[node]);
    else
    {
      dfs(it, index + 1);
      low[node] = min(low[node], low[it]);
      if(low[it] >= dfn[node])
      {
        int x;
        ++nr;
        result[nr].push_back(node);
        do{
          x = s.top();
          s.pop();
          result[nr].push_back(x);
        }while(x != it);
      }
    }
}

void solve()
{
  dfs(1, 1);
  g<<nr<<'\n';
  for(int i = 1;i <= nr;++i, g<<'\n')
    for(auto it : result[i])
      g<<it<<" ";
}

int main()
{
  read();
  solve();
  return 0;
}