Cod sursa(job #2984475)

Utilizator AlexBraileanuAlexandru Braileanu AlexBraileanu Data 24 februarie 2023 11:40:41
Problema Componente biconexe Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.74 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 , p , sz;
int idx[N] , low[N] , st[N];
int v[N2] , nxt[N2] , vf[N2];
char buff[N2];
void read(){
    fread(buff , 1 , N2 , fin);
    p = 0;
}
void get(int *x){
    while(!(buff[p] >= '0' && buff[p] <= '9')){
        ++p;
        if(p == N2)
            read();
    }
    *x = 0;
    while(buff[p] >= '0' && buff[p] <= '9'){
        *x = (*x) * 10 + (buff[p++] - '0');
        if(p == N2)
            read();
    }
}
void add(int x , int y){
    vf[++sz] = y;
    nxt[sz] = v[x];
    v[x] = 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[x] ; i ; i = nxt[i]){
        int y = 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");
    read() , get(&n) , get(&m);
    for(int i = 1 ; i <= m ; ++ i){
        get(&a) , get(&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[i + n] ; j ; j = nxt[j]){
            fprintf(fout , "%d " , vf[j]);
        }
        fprintf(fout , "\n");
    }
    fclose(fin);
    fclose(fout);
    return 0;
}