Cod sursa(job #3219967)

Utilizator thinkphpAdrian Statescu thinkphp Data 1 aprilie 2024 21:00:56
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#define FIN "sortaret.in"
#define FOUT "sortaret.out"
#define MAX 500005

struct Node {

      int info;
      struct Node *next;
};

int nodes,
    edges,
    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 );
          }
  }

  freopen(FOUT, "w", stdout);

  for(c = head; c; c = c->next) {

      printf("%d ", c->info);
  }

  return 0;
}