Cod sursa(job #607743)

Utilizator blustudioPaul Herman blustudio Data 13 august 2011 13:50:54
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
#include <vector>
using namespace std;

struct Node
{
	vector<unsigned long int> m_Vertices;
	bool m_Visited;
};

Node Graph[50000];
unsigned long int n, m, x, y, b;
vector<unsigned long int> Print;

void visit(unsigned long int t)
{
	if (Graph[t].m_Visited == false)
	{
		Graph[t].m_Visited = true;
		Print.push_back(t);
		for (unsigned long int i=0; i<Graph[t].m_Vertices.size(); i++)
		{
			visit(Graph[t].m_Vertices[i]);
		}
	}
}

int main()
{
	ifstream fin("sortaret.in");
	ofstream fout("sortaret.out");
	fin >> n >> m;
	fin >> x >> y;
	Graph[x].m_Vertices.push_back(y);
	b = x;
	for (unsigned long int i=1; i<m; i++)
	{
		fin >> x >> y;
		Graph[x].m_Vertices.push_back(y);
	}
	visit(b);
	for (unsigned long int i=0; i<Print.size(); i++)
		fout << Print[i] << " ";
	for (unsigned long int i=0; i<m; i++)
		if (Graph[i].m_Visited == false)
			fout << i << " ";
	fin.close();
	fout.close();
	return 0;
}