Cod sursa(job #3373837)

Utilizator Chris_BlackBlaga Cristian Chris_Black Data 7 noiembrie 2026 22:50:31
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.83 kb
#include <bits/stdc++.h>

#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define ff first
#define ss second
#define vpi vector<pii>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORR(i, a, b) for(int i = (a); i >= (b); --i)
#define pb push_back
using namespace std;
const int N = 1e6 + 9;
const bool testcases = false;

const string INPUT_FILE = "biconex";
ifstream fin(INPUT_FILE + ".in");
ofstream fout(INPUT_FILE + ".out");
#define cin fin
#define cout fout

int n, m;

vvi G(N);
int low[N], lvl[N];
vpi stk;
vector<unordered_set<int>> cc;

void Dfs(int x, int p = -1, int dep = 1) {
    low[x] = lvl[x] = dep;

    for (auto y : G[x]) {
        if (y == p)continue;

        if (!lvl[y]) {
            stk.pb({x, y});
            Dfs(y, x, dep + 1);
            low[x] = min(low[x], low[y]);

            if (low[y] >= lvl[x]) {
                cc.pb({});
                int a, b;
                do {
                    tie(a, b) = stk.back();
                    stk.pop_back();

                    cc.back().insert(a); cc.back().insert(b);
                } while (a != x || b != y);
            }
        } else low[x] = min(low[x], lvl[y]);
    }
}

void solve_testcase() {
    cin >> n >> m;

    {
        int x, y;
        FOR(i, 1, m) {
            cin >> x >> y;
            G[x].pb(y);
            G[y].pb(x);
        }
    }

    FOR(i, 1, n) if (!lvl[i]) Dfs(i);

    cout << cc.size() << '\n';
    for (auto e : cc) {
        for (auto f : e) cout << f << ' ';
        cout << '\n';
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int t = 1;
    if (testcases) cin >> t;
    while (t --) solve_testcase();
    return 0;
}