Cod sursa(job #1250469)

Utilizator serban.cobzacCobzac Serban serban.cobzac Data 28 octombrie 2014 10:13:42
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#include <vector>
#define dmax 100005
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");

vector <int> g[dmax];
vector <int> gt[dmax];
int n, m, postordine[dmax], nr;
bool uz[dmax];

void citire();
void rez();
void dfsG(int);
void dfsGt(int);

int main(){
    citire();
    rez();
    return 0;
}

void citire(){
    int x, y;
    fin>>n>>m;
    for(int i=1; i<=m; ++i){
        fin>>x>>y;
        g[x].push_back(y);
        gt[y].push_back(x);
    }
}

void rez(){
    int i;
    for(i=1; i<=n; ++i)
        if(!uz[i])
            dfsG(i);
    for(i=1; i<=n; ++i) uz[i]=0;
    for(i=n; i>0; --i)
        if(!uz[postordine[i]]){
            dfsGt(i);
            fout<<'\n';
        }
}

void dfsG(int vf){
    uz[vf]=1;
    int lg=g[vf].size();
    for(int i=1; i<=lg; ++i)
        if(!uz[ g[vf][i] ])
            dfsG(g[vf][i]);
    postordine[++nr]=vf;
}

void dfsGt(int vf){
    uz[vf]=1;
    fout<<vf<<' ';
    int lg=gt[vf].size();
    for(int i=1; i<=lg; ++i)
        if(!uz[ gt[vf][i] ])
            dfsGt(gt[vf][i]);
}