Cod sursa(job #3259772)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 27 noiembrie 2024 19:38:09
Problema Componente biconexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

ifstream cin ("biconex.in");
ofstream cout ("biconex.out");
const int N = 1e5;
int low[N + 1], discover[N + 1];

vector <int> g[N + 1], rez[N + 1];

stack <int> s;

stack <pair <int, int> > s_m;

int n, m, x, y, t, nr_comp;

void dfs (int node, int parent)
{
    low[node] = discover[node] = ++t;
    s.push (node);
    for (auto it : g[node])
    {
        if (!discover[it])
        {
            s_m.push({node, it}); /// muchiile bagate
            dfs (it, node);
            low[node] = min (low[node], low[it]);///timpul minim de a ajung la el e de a ajunge la fii sai
            if (low[it] > discover[node]);///(node, it) - bridge
            if (low[it] >= discover[node])///node - punct de articulatie
            {
                rez[++nr_comp].push_back(node);
                while (!s.empty() && s.top() != it)
                    rez[nr_comp].push_back(s.top()), s.pop();
                if (!s.empty())
                    rez[nr_comp].push_back(s.top()), s.pop();
            }
        }
        else
        {
            if (it != parent)///stramos(daca e muchie back)
                s_m.push({node, it}), low[node] = min (low[node], discover[it]);
        }
    }
}


int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs (1, 0);
    for (int i = 1; i <= nr_comp; ++i, cout << '\n')
        for (auto it : rez[i])
        cout << it << ' ';
    return 0;
}