Cod sursa(job #826474)

Utilizator zalmanDanci Emanuel Sebastian zalman Data 30 noiembrie 2012 19:40:38
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <cstdio>
#include <vector>
#include <stack>
#define NMAX 50005
using namespace std;

vector<int> G[NMAX], st;
int N, M;
bool viz[NMAX], ft = 1;

void read(void)
{
	int x, y;
	FILE *f = fopen("sortaret.in", "r");
		
	fscanf(f, "%d%d", &N, &M);	
	for(int i = 0; i < M ; ++i)	
		fscanf(f, "%d%d", &x, &y),
		G[x].push_back(y);
		
	fclose(f);
}

void DFS(int nod)
{
	viz[nod] = 1;
	for(int i = 0; i < G[nod].size(); ++i)
		if(!viz[G[nod][i]])
			st.push_back(G[nod][i]), DFS(G[nod][i]);
	
	
	//st.push(nod);
}

void solve(void)
{
	st.push_back(1);
	for(int i = 1; i <= N; ++i)
		if(!viz[i])
			DFS(i);
}

void print(void)
{
	FILE *g = fopen("sortaret.out", "w");
	
	/*
	while(!st.empty())
	{
		fprintf(g, "%d ", st.back());
		st.pop_back();
	}*/
	for(int i = 0; i < N; ++i)
		fprintf(g, "%d ", st[i]);
}

int main(void)
{
	read();
	solve();
	print();
	
	return 0;
}