Cod sursa(job #1454520)

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

using namespace std;

int color[maxn];
stack<int> S;
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;
	S.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 (!S.empty())
	{
		out << S.top() << " ";
		S.pop();
	}

	return 0;
}