Cod sursa(job #3217756)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 24 martie 2024 15:45:46
Problema Componente biconexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.36 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
ifstream cin("biconex.in");
ofstream cout("biconex.out");
using pii = pair<int,int>;
const int nmax = 1e5 + 1;
vector <int> g[nmax];
vector <vector <int>> bic;
int n , m , a , b , tm , tin[nmax] , st[nmax];
vector <int> un;
stack <int> s;
void dfs(int x , int p)
{
    tin[x] = ++tm;
    st[x] = tm;
    s.push(x);
    for(auto it : g[x])
    {
        if(it == p) continue;
        if(tin[it]) st[x] = min(st[x],tin[it]);
        else
        {
            dfs(it,x);
            st[x] = min(st[x],st[it]);
            if(st[it] >= tin[x])
            {
                un.clear();
                while(s.top() != it)
                {
                    un.push_back(s.top()); s.pop();
                }
                s.pop();
                un.push_back(it);
                un.push_back(x);
                bic.push_back(un);
            }
        }
    }
    s.pop();
}
int main()
{

    cin >> n >> m;
    for(int i = 1 ; i <= m ; ++i)
    {
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1,0);
    cout << bic.size() << '\n';
    for(auto it : bic)
    {
        for(auto j : it)
        {
            cout << j << ' ';
        }
        cout << '\n';
    }
    return 0;
}