Cod sursa(job #1290676)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 11 decembrie 2014 17:53:36
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.61 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>

using namespace std;

const int nmax = 100005;

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

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

void get_sol(int x, int y)
{
    sol++;
    while(st.back() != y)
    {
        comp[sol].push_back(st.back());
        st.pop_back();
    }
    st.pop_back();
    comp[sol].push_back(x);
    comp[sol].push_back(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;
}