Cod sursa(job #640301)

Utilizator toniobFMI - Barbalau Antonio toniob Data 25 noiembrie 2011 10:39:59
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <vector>
using namespace std;

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

int n, m, v[50002];
vector <int> a[50002];
bool marcat [50002];

void citire () {
    int x, y;
    in >> n >> m;
    while (m--) {
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
}

void dfs (int x) {
    marcat[x] = true;

    for (int i = 0; i < a[x].size(); ++i) {
        if (!marcat[a[x][i]]) {
            dfs(a[x][i]);
        }
    }

    v[++v[0]] = x;
}

int main () {
    citire ();

    for (int i = 1; i <= n; ++i) {
        if (!marcat[i]) {
            dfs(i);
        }
    }

    for (int i = n; i >= 1; --i) {
        out << v[i] << ' ';
    }
    out << '\n';

    return 0;
}