Cod sursa(job #627242)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 29 octombrie 2011 13:28:07
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <stdio.h>
int n,m;
struct nod{
    int y;
    struct nod*next; }*g[50001],*a;

bool viz[50001];

void add(int x,int y){
    nod*a=new nod;
    a->y=y;
    a->next=g[x];
    g[x]=a;
}

void read_data(){
    int i,x,y;
        freopen("sortaret.in","r",stdin);
        scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
            add(x,y);}
}

void DFS(int x){
    viz[x]=1; nod*v=g[x];
    while(v!=NULL){
        if(!viz[v->y])DFS(v->y);
        v=v->next;}
    nod*c=new nod;
    c->y=x; c->next=a; a=c;
}

void process(){
    int i;
    for(i=1;i<=n;i++)
    if(!viz[i])DFS(i);
}

void write_data(){
    freopen("sortaret.out","w",stdout);
    while(a!=NULL){
        printf("%d ",a->y);
        a=a->next;};
}

int main(){
    read_data();
    process();
    write_data();
}