Cod sursa(job #2984472)

Utilizator AlexBraileanuAlexandru Braileanu AlexBraileanu Data 24 februarie 2023 11:32:40
Problema Componente biconexe Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <stdio.h>
#include <stdlib.h>
#define N 100001
#define N2 (8*N)
#define min(x,y) (x<y?x:y)
FILE *fin , *fout;
int n , m , a , b , c , k , B;
int idx[N] , low[N] , st[N];
struct V{
    int sz;
    int v[N2] , nxt[N2] , vf[N2];
}v;
void add(int x , int y){
    v.vf[++v.sz] = y;
    v.nxt[v.sz] = v.v[x];
    v.v[x] = v.sz;
}
void bag(int x , int y){
    ++B;
    while(k > 0 && st[k] != y)
        add(B + n , st[k--]);
    add(B + n , st[k--]);
    add(B + n , x);
}
void dfs(int x){
    idx[x] = low[x] = ++c;
    st[++k] = x;
    for(int i = v.v[x] ; i ; i = v.nxt[i]){
        int y = v.vf[i];
        if(!idx[y]){
            dfs(y);
            low[x] = min(low[x] , low[y]);
            if(low[y] >= idx[x]){
                bag(x , y);
            }
        }else{
            low[x] = min(low[x] , idx[y]);
        }
    }
}
int main(){
    fin = fopen("biconex.in" , "r");
    fout = fopen("biconex.out" , "w");
    fscanf(fin , "%d%d" , &n , &m);
    for(int i = 1 ; i <= m ; ++ i){
        fscanf(fin , "%d%d" , &a , &b);
        add(a , b);
        add(b , a);
    }
    for(int i = 1 ; i <= n ; ++ i)
        if(!idx[i])
            dfs(i);
    fprintf(fout , "%d\n" , B);
    for(int i = 1 ; i <= B ; ++ i){
        for(int j = v.v[i + n] ; j ; j = v.nxt[j]){
            fprintf(fout , "%d " , v.vf[j]);
        }
        fprintf(fout , "\n");
    }
    fclose(fin);
    fclose(fout);
    return 0;
}