Cod sursa(job #1383980)

Utilizator koroalinAlin Corodescu koroalin Data 10 martie 2015 19:46:38
Problema Componente biconexe Scor 52
Compilator cpp Status done
Runda Arhiva educationala Marime 1.98 kb
#include <fstream>
#include <vector>
#include <cstdio>
#define NMAX 100001
#define MMAX 200001
using namespace std;
ifstream fin("biconex.in");
FILE* fout=fopen("biconex.out","w");
int n,m,dfn[NMAX],low[NMAX],nrfii,vf,nrb,nr;
pair <int,int> stiva[MMAX];
bool art[NMAX];
vector <int> G[NMAX];
vector <pair <int,int> > sol[MMAX];
void citire();
void dfs(int x,int tx);
void afiseaza(int x,int tx);
int main()
{
    int i,j;
    citire();
    stiva[++vf]=make_pair(1,0);
    dfs(1,0);
    fprintf(fout,"%d\n",nrb);
    for ( i=1; i<=nrb; i++)
    {
        fprintf(fout,"%d ",sol[i][0].first);
        for ( j=1; j<sol[i].size()-1; j++)
           fprintf(fout,"%d ",sol[i][j].first);
        if (sol[i].size() > 1 ) fprintf(fout,"%d",sol[i][sol[i].size()-1].first);
        else fprintf(fout,"%d",sol[i][sol[i].size()-1].second);
        fprintf(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 i;
    dfn[x]=++nr; low[x]=dfn[x];
    art[x]=0;
    for (i=0; i<G[x].size(); ++i)
    {
        if (G[x][i]!=tx && dfn[G[x][i]]<dfn[x]) stiva[++vf]=make_pair(G[x][i],x);
        if (!dfn[G[x][i]])
        {
            if (!tx) nrfii++;
            dfs(G[x][i],x);
            if (low[x]>low[G[x][i]]) low[x]=low[G[x][i]];
            if (low[G[x][i]]>=dfn[x])
            {
                if (tx)
                    art[x]=1;
                afiseaza(G[x][i],x);
            }
        }
        else
        {
            if (G[x][i]!=tx)
            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) );
}