Cod sursa(job #3226529)

Utilizator alex_0747Gheorghica Alexandru alex_0747 Data 21 aprilie 2024 18:16:57
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <bitset>
#include <stack>
#include <set>
using namespace std;

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

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

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;
    tin[k] = low[k] = 1 + tin[ant];
    for (int i : L[k])
    {
        if (i == ant) continue;
        if (v[i] == 1)
            low[k] = min(low[k], tin[i]);
        else
        {
            st.push({ k, i });
            DFS(i, k);
            low[k] = min(low[k], low[i]);
            if (tin[k] <= low[i])
                Component(k, i);
        }
    }
}

int main()
{   
    int i, x, y;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }

    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;
}