Cod sursa(job #1454515)

Utilizator starduststardust stardust Data 26 iunie 2015 18:48:56
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
#define maxn 50010
#define ALB 0
#define GRI 1
#define NEGRU 2

using namespace std;

int color[maxn];
queue<int> Q;
vector<int> a[maxn];

int n, m;

void dfs(int node)
{
	color[node] = GRI;
	for (int i = 0; i < a[node].size(); i++)
		if (color[a[node][i]] == ALB)
			dfs(a[node][i]);

	color[node] = NEGRU;
	Q.push(node);
}

int main()
{
	ifstream in("sortaret.in");
	ofstream out("sortaret.out");

	in >> n >> m;
	int x, y;

	for (int i = 0; i < m; i++)
	{
		in >> x >> y;
		a[x].push_back(y);
	}

	for (int i = 1; i < n; i++)
	{
		if (color[i] == ALB)
			dfs(i);
	}

	while (!Q.empty())
	{
		out << Q.front() << " ";
		Q.pop();
	}

	return 0;
}