Cod sursa(job #575161)

Utilizator mihai995mihai995 mihai995 Data 7 aprilie 2011 23:11:38
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fstream>
#include <vector>
using namespace std;

const int N=50001;
int need[N],n,m;
bool use[N];
vector<int> a[N];

ifstream in("sortaret.in");
ofstream out("sortaret.out");

void mark(int x)
{
	use[x]=true;
	out<<x<<" ";
	for (int i=0;i<a[x].size();i++)
	{
		int y=a[x][i];
		need[y]--;
		if (!need[y])
			mark(y);
	}
}

int main()
{
	int x,y;
	in>>n>>m;
	while (m--)
	{
		in>>x>>y;
		a[x].push_back(y);
		need[y]++;
	}
	for (int i=1;i<=n;i++)
		if (!use[i] && !need[i])
			mark(i);
	out<<"\n";
	return 0;
}