Cod sursa(job #2704010)

Utilizator teofilotopeniTeofil teofilotopeni Data 9 februarie 2021 17:32:00
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <stdio.h>
#include <vector>
#include <stack>
#include <bitset>
using namespace std;

//  Sortare topologica

vector<stack<int>> nodes;
bitset<50010> vizitat;
stack<int> coada;

void parcurge(int index) {
    vizitat[index] = true;
    for (; nodes[index].size(); nodes[index].pop())
        if (!vizitat[nodes[index].top()])
            parcurge(nodes[index].top());
    coada.push(index);
}

int main() {
	FILE* stream;
	freopen_s(&stream, "sortaret.in", "r", stdin);
	freopen_s(&stream, "sortaret.out", "w", stdout);
	int n, m, x, y;
	scanf("%d %d", &n, &m);
    nodes = vector<stack<int>>(n + 1);
	while (n--) {
        scanf("%d %d", &x, &y);
        nodes[x].push(y);
        nodes[y].push(x);
	}
	for (parcurge(1); coada.size(); coada.pop())
        cout << coada.top() << ' ';
	return 0;
}