Cod sursa(job #2984441)

Utilizator AlexBraileanuAlexandru Braileanu AlexBraileanu Data 24 februarie 2023 10:49:26
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
int const N = 2e5 + 3;
int n , m , a , b , c , k , B;
int idx[N] , low[N] , st[N];
vector<int> v[N] , bcx[N];
void baga_biconexa(int x , int y){
    ++B;
    while(k > 0 && st[k] != y){
        bcx[B].push_back(st[k--]);
    }
    bcx[B].push_back(st[k--]);
    bcx[B].push_back(x);
}
void dfs(int x){
    idx[x] = low[x] = ++c;
    st[++k] = x;
    for(int y : v[x]){
        if(!idx[y]){
            dfs(y);
            low[x] = min(low[x] , low[y]);
            if(low[y] >= idx[x]){
                baga_biconexa(x , y);
            }
        }else{
            low[x] = min(low[x] , idx[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);
    }
    for(int i = 1 ; i <= n ; ++ i){
        if(!idx[i])
            dfs(i);
    }
    fout << B << '\n';
    for(int i = 1 ; i <= B ; ++ i){
        for(int y : bcx[i])
            fout << y << ' ';
        fout << '\n';
    }
    return 0;
}