Cod sursa(job #1384048)

Utilizator koroalinAlin Corodescu koroalin Data 10 martie 2015 20:26:43
Problema Componente biconexe Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.92 kb
#include <fstream>
#include <vector>
#include <cstdio>
#include <algorithm>
#define NMAX 100001
#define MMAX 200001
using namespace std;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
int n,m,dfn[NMAX],low[NMAX],nrfii,vf,nrb,nr;
pair <int,int> stiva[MMAX];
bool art[NMAX],uz[NMAX];
vector <int> G[NMAX];
vector <pair <int,int> > sol[MMAX];
void citire();
void dfs(int x,int tx,int nr);
void afiseaza(int x,int tx);
int main()
{
    int i,j;
    citire();
    stiva[++vf]=make_pair(1,0);
    dfs(1,0,1);
    fout<<nrb<<'\n';
    for ( i=1; i<=nrb; i++)
    {
        for (j=1; j<=n; j++) uz[j]=0;
        for (j=0; j<sol[i].size();j++)
        {
           // sort(sol[i].begin(),sol[i].end());
            if (!uz[sol[i][j].first] && sol[i][j].first)
            {
                fout<<sol[i][j].first<<" ";
                uz[sol[i][j].first]=1;
            }
            if (!uz[sol[i][j].second] && sol[i][j].second)
            {
                fout<<sol[i][j].second<<" ";
                uz[sol[i][j].second]=1;
            }
        }
        fout<<'\n';
    }
    return 0;
}
void citire()
{
    int a,b,i;
    fin>>n>>m;
    for (i=1; i<=m; i++)
    {
        fin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
}
void dfs(int x,int tx,int nr)
{
    int i;
    dfn[x]=nr; low[x]=dfn[x];
    //art[x]=0;
    for (i=0; i<G[x].size(); ++i)
    {
        if (G[x][i]==tx) continue;
        if (!dfn[G[x][i]])
        {
            stiva[++vf]=make_pair(G[x][i],x);
            dfs(G[x][i],x,nr+1);
            if (low[x]>low[G[x][i]]) low[x]=low[G[x][i]];
            if (low[G[x][i]]>=dfn[x])
                afiseaza(G[x][i],x);
        }
        else
            if (low[x]>dfn[G[x][i]])
                low[x]=dfn[G[x][i]];
    }
}
void afiseaza(int x,int tx)
{
    pair <int,int> p;
    nrb++;
    do
    {
        p=stiva[vf];
        sol[nrb].push_back(p);
        vf--;
    }
    while ( !(p.first==x && p.second==tx) );
}