Cod sursa(job #1457091)

Utilizator valentin50517Vozian Valentin valentin50517 Data 2 iulie 2015 17:34:55
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>

using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");

typedef struct nod{
	int key;
	nod* next;
	nod(){
		next = NULL;
	}
} *list;

list A[50005],rs;
bool B[50005];
void add(list &a,int key){
	list b = new nod;
	b->key = key;
	b->next = a;
	a = b;
}
void DBS(int key){
	B[key] = true;
	for(list l = A[key];l;l = l->next) if(!B[l->key]) DBS(l->key);
	add(rs,key);
}
int N,M,x,y;
int main(){
	fin >> N >> M;
	for(int i = 0;i<M;i++){
		fin >> x >> y;
		add(A[x],y);
	}	
	for(int i = 1;i<=N;i++){
		if(!B[i]) DBS(i);
	}
	
	for(list l = rs;l;l = l->next) fout << l->key << ' ';
	return 0;
}