Cod sursa(job #3377339)

Utilizator PuternicDezvoltatPuternicSRL PuternicDezvoltat Data 22 noiembrie 2026 22:32:14
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.88 kb
// https://www.infoarena.ro/problema/biconex
// Complexitate: O(N + M) - Tarjan: un singur DFS cu niv/low si o stiva de noduri
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 100005;

FILE *fin = fopen("biconex.in", "r");
char buf[1 << 16];
int bpos = 0, blen = 0;

char urm() {   // urmatorul caracter din fisier (citire rapida pe bucati)
    if (bpos == blen) {
        blen = fread(buf, 1, sizeof(buf), fin);
        bpos = 0;
        if (blen <= 0) return 0;
    }
    return buf[bpos++];
}

int citeste() {
    char c = urm();
    while (c && (c < '0' || c > '9')) c = urm();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + (c - '0');
        c = urm();
    }
    return x;
}

int n, m, timp;
vector<int> g[NMAX];
int niv[NMAX], low[NMAX], tata[NMAX];
unsigned poz[NMAX];                 // pana unde am parcurs lista de vecini a nodului
vector<int> stiva;                  // nodurile vizitate, pentru extragerea componentelor
vector<vector<int>> componente;

// DFS iterativ (fara recursivitate => nu depaseste stiva la N = 100000)
void dfs(int rad) {
    vector<int> st = {rad};
    niv[rad] = low[rad] = ++timp;
    stiva.push_back(rad);
    while (!st.empty()) {
        int nod = st.back();
        if (poz[nod] < g[nod].size()) {
            int vecin = g[nod][poz[nod]++];
            if (vecin == tata[nod]) continue;
            if (niv[vecin]) {                       // deja vizitat => muchie de intoarcere
                low[nod] = min(low[nod], niv[vecin]);
            } else {                                // coboram in vecin
                tata[vecin] = nod;
                niv[vecin] = low[vecin] = ++timp;
                stiva.push_back(vecin);
                st.push_back(vecin);
            }
        } else {                                    // am terminat nodul => revenim in tata
            st.pop_back();
            int p = tata[nod];
            if (p == 0) continue;
            low[p] = min(low[p], low[nod]);
            if (low[nod] >= niv[p]) {               // p separa subarborele lui nod
                // scoatem din stiva pana la nod (inclusiv) + p => o componenta
                componente.push_back({p});
                while (stiva.back() != nod) {
                    componente.back().push_back(stiva.back());
                    stiva.pop_back();
                }
                componente.back().push_back(nod);
                stiva.pop_back();
            }
        }
    }
    stiva.clear();
}

int main() {
    n = citeste();
    m = citeste();
    for (int i = 0; i < m; i++) {
        int x = citeste(), y = citeste();
        g[x].push_back(y);
        g[y].push_back(x);
    }

    for (int i = 1; i <= n; i++)
        if (!niv[i]) dfs(i);

    ofstream fout("biconex.out");
    fout << componente.size() << "\n";
    for (auto &c : componente) {
        for (int x : c) fout << x << " ";
        fout << "\n";
    }
    return 0;
}