Cod sursa(job #903542)

Utilizator visanrVisan Radu visanr Data 1 martie 2013 22:04:33
Problema Componente biconexe Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.69 kb
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;

#define Nmax 100010
#define PI pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair

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

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

void DFS(int Node, int Father = 0)
{
    Low[Node] = Level[Node];
    vector<int> :: iterator it;
    for(it = G[Node].begin(); it != G[Node].end(); ++it)
    {
        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] == 0)
        {
            Level[i] = 1;
            DFS(i);
        }
    printf("%i\n", Biconnected.size());
    for(i = 0; i < Biconnected.size(); printf("\n"), i ++)
        for(j = 0; j < Biconnected[i].size(); j ++)
            printf("%i ", Biconnected[i][j]);
    return 0;
}