Cod sursa(job #700904)

Utilizator hunter_ionutzzzFarcas Ionut hunter_ionutzzz Data 1 martie 2012 12:30:25
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include<fstream>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
typedef struct nod{int val;
                   nod *next;
};
int a,b,n,m,grad[50001],cd[100001],i;
nod *q,*v[50001];
int main()
{   fin >> n >> m;
    for (i=1;i<=m;++i)
	{   fin >> a >> b;
	    q = new nod;
		q -> val = b;
		q -> next = v[a];
		v[a] = q;
	    ++grad[b];
	}
	int x=1;
	while (grad[x])
		++x;
	int ic=0,sc=1;
	cd[1] = x;
	while (ic<=sc)
	{   ++ic;
	    q = v[cd[ic]];
		while (q)
		{   cd[++sc] = q -> val;
		    q = q -> next;
		}
		fout << cd[ic] << " ";
	}
	return 0;
}