Cod sursa(job #2084271)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 8 decembrie 2017 21:19:19
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 50005;
vector<int> ls[nmax];
int viz[nmax], x, y, n, m, stiva[nmax], vf, i;

void dfs(int x) {
    int l = ls[x].size(), i, y;
    viz[x] = 1;
    for (i = 0; i < l; i++) {
        y = ls[x][i];
        if (viz[y]) continue;
        dfs(y);
    }
    stiva[++vf] = x;
}

int main() {
    f >> n >> m;
    while (m--) {
        f >> x >> y;
        ls[x].push_back(y);
        ls[y].push_back(x);
    }
    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
            dfs(i);
    while (vf)
        g<<stiva[vf--]<<' ';
}