Cod sursa(job #2195698)

Utilizator flibiaVisanu Cristian flibia Data 17 aprilie 2018 10:19:29
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

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

int x, y, n, m, st[50100], vf;
vector <int> v[50100];
bitset <50100> viz;

void dfs(int from){
	viz[from] = 1;
	for(auto to : v[from])
		if(!viz[to])
			dfs(to);
	st[++vf] = from;
}

int main(){
	in >> n >> m;
	while(m--){
		in >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for(int i = 1; i <= n; i++)
		if(!viz[i])
			dfs(i);
	for(; vf; vf--)
		out << st[vf] << ' ';
	return 0;
}