Cod sursa(job #3226373)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 21 aprilie 2024 11:35:10
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.25 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize ("O1")
#pragma GCC optimize ("O2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree <long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update>
#define lsb(x)(x & (-x))

const int max_size = 1e5 + 20;

int viz[max_size], tin[max_size], tmin[max_size];
vector <int> mc[max_size];
vector <vector <int>> cbc;
stack <int> stk;

void dfs (int nod, int par)
{
    viz[nod] = 1;
    tin[nod] = tin[par] + 1;
    tmin[nod] = tin[nod];
    stk.push(nod);
    for (auto f : mc[nod])
    {
        if (f == par)
        {
            continue;
        }
        if (viz[f] == 1)
        {
            tmin[nod] = min(tmin[nod], tin[f]);
        }
        else
        {
            dfs(f, nod);
            tmin[nod] = min(tmin[nod], tmin[f]);
            if (tin[nod] <= tmin[f])
            {
                vector <int> aux;
                while (stk.top() != f)
                {
                    aux.push_back(stk.top());
                    stk.pop();
                }
                aux.push_back(f);
                aux.push_back(nod);
                stk.pop();
                cbc.push_back(aux);
            }
        }
    }
}

void solve ()
{
    int n, m;
    cin >> n >> m;
    while (m--)
    {
        int x, y;
        cin >> x >> y;
        mc[x].push_back(y);
        mc[y].push_back(x);
    }
    dfs(1, 0);
    cout << cbc.size() << '\n';
    for (auto f : cbc)
    {
        for (auto ff : f)
        {
            cout << ff << " ";
        }
        cout << '\n';
    }
    cout << '\n';
}

signed main ()
{
#ifdef LOCAL
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#else
    freopen("biconex.in", "r", stdin);
    freopen("biconex.out", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long tt;
    //cin >> tt;
    tt = 1;
    while (tt--)
    {
        solve();
    }
    return 0;
}