Cod sursa(job #933674)

Utilizator visanrVisan Radu visanr Data 30 martie 2013 11:48:04
Problema Componente biconexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;


#define Nmax 100010
#define forit(it, v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); ++ it)
#define PI pair<int, int>
#define pb push_back
#define mp make_pair
#define f first
#define s second

int N, M, X, Y, Low[Nmax], Level[Nmax];
vector<int> G[Nmax], CrtB;
vector<vector<int> > Bic;
stack<PI> S;

void GetComp(int X, int Y)
{
    CrtB.clear();
    PI Now;
    do
    {
        Now = S.top();
        S.pop();
        CrtB.pb(Now.s);
    }while(Now.f != X && Now.s != Y);
    CrtB.pb(Now.f);
    Bic.pb(CrtB);
}

void DFS(int Node, int Father = 0)
{
    Low[Node] = Level[Node];
    forit(it, G[Node])
    {
        if(*it == Father) continue;
        if(Level[*it] == 0)
        {
            S.push(mp(Node, *it));
            Level[*it] = Level[Node] + 1;
            DFS(*it, Node);
            Low[Node] = min(Low[Node], Low[*it]);
            if(Low[*it] >= Level[Node]) GetComp(Node, *it);
        }else Low[Node] = min(Low[Node], Low[*it]);
    }
}

int main()
{
    freopen("biconex.in", "r", stdin);
    freopen("biconex.out", "w", stdout);
    int i, j;
    scanf("%i %i", &N, &M);
    for(i = 1; i <= M; ++ i)
    {
        scanf("%i %i", &X, &Y);
        G[X].pb(Y);
        G[Y].pb(X);
    }
    for(i = 1; i <= N; ++ i)
        if(!Level[i])
        {
            Level[i] = 1;
            DFS(i, 0);
        }
    printf("%i\n", Bic.size());
    for(i = 0; i < Bic.size(); ++ i)
    {
        for(j = 0; j < Bic[i].size(); ++ j)
            printf("%i ", Bic[i][j]);
        printf("\n");
    }
    return 0;
}