Cod sursa(job #2330026)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 27 ianuarie 2019 19:24:39
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>
using namespace std;

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

const int DIM = 5e4 + 7;

vector <int> v[DIM];
vector <int> st;

bitset <DIM> vis;

void dfs(int nod)
{
	vis[nod] = 1;

	for(int i = 0; i < v[nod].size(); i++)
		if(vis[v[nod][i]] == 0)
		{
			dfs(v[nod][i]);
		}

	st.push_back(nod);
}

int main()
{
	int n, m;
	in >> n >> m;

	while(m--)
	{
		int x, y;
		in >> x >> y;

		v[x].push_back(y);
	}

	for(int i = 1; i <= n; i++)
        if(vis[i] == 0)
            dfs(i);

	for(int i = st.size() - 1; i >= 0; i--)
	{
		out << st[i] << ' ';
	}
}