Cod sursa(job #3231483)

Utilizator TrifoitaBejenescu-Babusanu Stefan Trifoita Data 26 mai 2024 19:35:23
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>
#define FIN "sortaret.in"
#define FOUT "sortaret.out"
#define MAX 50005

struct Node {

      int info;
      struct Node *next;
};

int nodes, //numarul de noduri
    edges,//numarul de arce
    visited[MAX];


struct Node *G[MAX],
       *head = NULL;


void DFS(int node) {

     struct Node *c, *p;

     visited[ node ] = 1;

     for(c = G[node]; c; c = c->next) {

             if(!visited[c->info]) {

                DFS(c->info);
             }
     }


     p = (struct Node*)malloc(sizeof(struct Node));
     p->info = node;
     p->next = head;
     head = p;
}

int main(int argc, char const *argv[]) {


    int i, j;

    struct Node *c;

    freopen(FIN, "r", stdin);

    scanf("%d %d",&nodes, &edges);


    while(edges--) {

         scanf("%d %d", &i, &j);
         c = (struct Node*)malloc(sizeof(struct Node));
         c->info = j;
         c->next = G[i];
         G[i] = c;
    }


   for(int node = 1; node <= nodes; ++node) {

           if(!visited[ node ]) {

               DFS( node );
           }
   }

   printf("Sortare topologica:\n");

   for(c = head; c; c = c->next) printf("%d ", c->info);

    return 0;
}