Cod sursa(job #1377444)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 5 martie 2015 21:46:08
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

using namespace std;

const int nmax = 100005;

int n, m, x, y, i, cnt, sol, dft[nmax], low[nmax];

vector<int> st, v[nmax], comp[nmax];

void get_sol(int x, int y)
{
    sol++;
    while(st.back() != y)
    {
        comp[sol].pb(st.back());
        st.pop_back();
    }
    st.pop_back();
    comp[sol].pb(x);
    comp[sol].pb(y);
}

void dfs(int x, int f)
{
    dft[x] = low[x] = ++cnt;

    for(auto it : v[x])
        if(it != f)
        {
            if(!dft[it])
            {
                st.pb(it);
                dfs(it, x);
                low[x] = min(low[x], low[it]);
                if(low[it] >= dft[x]) get_sol(x, it);
            }
            else low[x] = min(low[x], dft[it]);
        }
}

int main()
{
    freopen("biconex.in", "r", stdin);
    freopen("biconex.out", "w", stdout);

    scanf("%d%d", &n, &m);

    for(; m; m--)
    {
        scanf("%d%d", &x, &y);
        v[x].pb(y);
        v[y].pb(x);
    }

    dfs(1, 0);

    printf("%d\n", sol);
    for(i = 1; i <= sol; i++)
    {
        for(auto it : comp[i]) printf("%d ", it);
        printf("\n");
    }

    return 0;
}