Cod sursa(job #3289825)

Utilizator alex_0747Gheorghica Alexandru alex_0747 Data 28 martie 2025 17:17:12
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("biconex.in");
ofstream fout("biconex.out");

vector <int> L[100005], art;
vector <set<int> > comp;
int depth[100005], low[100005], n, m, t;
bitset <100005> v;
stack <pair<int, int> > st;

void Component(int x, int y)
{
    int tx, ty;
    set <int> b;
    do 
    {
        tx = st.top().first;
        ty = st.top().second;
        st.pop();
        b.insert(tx);
        b.insert(ty);
    }while (tx != x && ty != y);
    comp.push_back(b);
}

void DFS(int k, int ant)
{
    v[k] = 1;
    depth[k] = low[k] = 1 + depth[ant];
    for (int i : L[k])
    {
        if (i == ant) continue;
        if (v[i] == 1)
            low[k] = min(low[k], depth[i]);
        else
        {
            st.push({k, i});
            DFS(i, k);
            low[k] = min(low[k], low[i]);
            if (depth[k] <= low[i])
            {
                art.push_back(k);
                Component(k, i);
            }
        }
    }
}

int main()
{
    int i, j;
    fin >> n >> m;
    while (m--)
    {
        fin >> i >> j;
        L[i].push_back(j);
        L[j].push_back(i);
    }

    for (i = 1; i <= n; i++)
        if (!v[i])
            DFS(i, i);
    
    fout << comp.size() << "\n";
    for (i = 0; i < comp.size(); i++)
    {
        for (int j : comp[i])
            fout << j << " ";
        fout << "\n";
    }
    return 0;    
}