Cod sursa(job #3030085)

Utilizator DordeDorde Matei Dorde Data 17 martie 2023 15:11:15
Problema Componente biconexe Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
int const N = 1e5 + 3;
int n , m , a , b , c , vf;
int h[N] , low[N] , viz[N] , st[N];
vector<int> v[N];
vector<int> comp[N];
void bag_comp(int x , int y){
    ++c;
    while(vf && st[vf] != y){
        comp[c].push_back(st[vf--]);
    }
    comp[c].push_back(st[vf--]);
    comp[c].push_back(x);
}
void dfs(int x , int t){
    viz[x] = 1;
    low[x] = h[x];
    st[++vf] = x;
    for(int y : v[x]){
        if(y == t)
            continue;
        h[y] = 1 + h[x];
        if(!viz[y]){
            dfs(y , x);
            if(low[y] >= low[x]){
                bag_comp(x , y);
            }
        }
        low[x] = min(low[x] , low[y]);
    }
}
int main(){
    fin >> n >> m;
    for(int i = 1 ; i <= m ; ++ i){
        fin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1 , 0);
    fout << c << '\n';
    for(int i = 1 ; i <= c ; ++ i){
        for(int y : comp[i])
            fout << y << ' ';
        fout << '\n';
    }
    return 0;
}