Cod sursa(job #2340413)

Utilizator DariusDCDarius Capolna DariusDC Data 10 februarie 2019 13:53:45
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.68 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define MAX 100001

using namespace std;

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

int n, m;

vector <int> edge[MAX];
stack <int> st;

int low[MAX];
int nivel[MAX];
bool viz[MAX];

vector <int> sol[MAX];
int NrComponente;

void citeste()
{
    fin >> n >> m;
    while (m--)
    {
        int x, y;
        fin >> x >> y;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
}

void dfs(int nod)
{
    viz[nod] = true;
    low[nod] = nivel[nod];
    st.push(nod);
    for (unsigned int i = 0; i < edge[nod].size(); i++)
    {
        int vecin = edge[nod][i];
        if (viz[vecin] == true)
        {
            low[nod] = min(low[nod], nivel[vecin]);
            continue;
        }
        nivel[vecin] = nivel[nod] + 1;
        dfs(vecin);
        low[nod] = min(low[nod], low[vecin]);
        if (low[vecin] >= nivel[nod])
        {
            NrComponente++;
            sol[NrComponente].push_back(nod);
            int x = 0;
            do
            {
                x = st.top();
                st.pop();
                sol[NrComponente].push_back(x);
            }while (x != vecin);
        }
    }
}

int main()
{
    citeste();
    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
        {
            nivel[i] = 1;
            dfs(i);
        }
    }
    fout << NrComponente << "\n";
    for (int i = 1; i <= NrComponente; i++)
    {
        for (unsigned int k = 0; k < sol[i].size(); k++)
        {
            fout << sol[i][k] << " ";
        }
        fout << "\n";
    }
    return 0;
}