Cod sursa(job #3374324)

Utilizator MariaB144Maria Belceanu MariaB144 Data 8 noiembrie 2026 22:44:01
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

ifstream f("biconex.in");
ofstream g("biconex.out");

vector<int> a[100001], comp[100001], st;
int niv[100001], low[100001];
int n, m, timp, nrc;

void scoate(int x, int y)
{
    int nod;
    nrc++;
    do
    {
        nod = st.back();
        st.pop_back();
        comp[nrc].push_back(nod);
    } while (nod != y);
    comp[nrc].push_back(x);
}

void dfs(int x, int tata)
{
    int i, y;
    niv[x] = low[x] = ++timp;
    st.push_back(x);
    for (i = 0; i < a[x].size(); i++)
    {
        y = a[x][i];
        if (y == tata)
            continue;
        if (niv[y] != 0)
            low[x] = min(low[x], niv[y]);
        else
        {
            dfs(y, x);
            low[x] = min(low[x], low[y]);
            if (low[y] >= niv[x])
                scoate(x, y);
        }
    }
}

int main()
{
    int i, j, x, y;
    f >> n >> m;
    for (i = 1; i <= m; i++)
    {
        f >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for (i = 1; i <= n; i++)
        if (niv[i] == 0)
            dfs(i, 0);
    g << nrc << "\n";
    for (i = 1; i <= nrc; i++)
    {
        for (j = 0; j < comp[i].size(); j++)
            g << comp[i][j] << " ";
        g << "\n";
    }
    return 0;
}