Cod sursa(job #3222553)

Utilizator matei__bBenchea Matei matei__b Data 10 aprilie 2024 20:53:21
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.09 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 1'000'000'007
#define dim 100005
#define lim 1000000
#define mdim 1501
#define mult 2e9
#define maxx 200002
#define simaimult 1e17
#define NMAX 1505
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define pli pair<ll,int>
#define pil pair<int,ll>
#define piii pair<int,pair<int,int> >
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace std;

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

int n,m;
vector<int> g[dim];
vector<vector<int> > comp;
vector<int> aux;

bool viz[dim];
int low[dim];
int tin[dim],timer;
bool art[dim];
stack<pii> d;

void pop_until(int u,int v)
{
    set<int> s;

    while(d.top().first!=u || d.top().second!=v)
    {
        s.insert(d.top().first);
        s.insert(d.top().second);
        d.pop();
    }

    s.insert(d.top().first);
    s.insert(d.top().second);
    d.pop();

    aux.clear();

    for(auto it:s)
        aux.pb(it);

    comp.pb(aux);
}

void dfs(int nod,int daddy=0)
{
    viz[nod]=1;
    tin[nod]=low[nod]=++timer;

    for(auto it:g[nod])
    {
        if(it==daddy)
            continue;

        if(viz[it])
        {
            low[nod]=min(low[nod],tin[it]);
        }
        else 
        {
            d.push(mp(nod,it));
            dfs(it,nod);
            low[nod]=min(low[nod],low[it]);

            if(low[it]>=tin[nod])
            {
                pop_until(nod,it);
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    fin >> n >> m;

    for(int i=1; i<=m; i++)
    {
        int x,y;

        fin >> x >> y;

        g[x].pb(y);
        g[y].pb(x);
    }

    dfs(1);

    fout << comp.size() << "\n";

    for(auto i:comp)
    {
        for(auto nod:i)
            fout << nod << " ";

        fout << "\n";
    }

    return 0;
}