Cod sursa(job #1250477)

Utilizator serban.cobzacCobzac Serban serban.cobzac Data 28 octombrie 2014 10:20:15
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
//#include <fstream>
#include <cstdio>
#include <vector>
#define dmax 100005
using namespace std;
//ifstream fin("ctc.in");
//ofstream fout("ctc.out");
FILE*fin=fopen("ctc.in", "r");
FILE*fout=fopen("ctc.out", "w");

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;
    fscanf(fin, "%d%d", &n, &m);
    for(int i=1; i<=m; ++i){
        //fin>>x>>y;
        fscanf(fin, "%d%d", &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';
            fprintf(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<<' ';
    fprintf(fout, "%d ", vf);
    int lg=gt[vf].size();
    for(int i=1; i<=lg; ++i)
        if(!uz[ gt[vf][i] ])
            dfsGt(gt[vf][i]);
}