Cod sursa(job #2642876)

Utilizator JackstilAdascalitei Alexandru Jackstil Data 17 august 2020 14:29:52
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
#define nmax 50001

using namespace std;

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

int n, m, x, y, j;
vector <int> graf[nmax];
bool vizitat[nmax];

void dfs(int nod) {
    vizitat[nod] = true;
    out << nod << " ";

    for (unsigned int i = 0; i < graf[nod].size(); ++i) {
        j = graf[nod][i];

        if (!vizitat[j]) {
            vizitat[j] = true;
            out << j << " ";
        }
    }
}

void citire() {
    in >> n >> m;
    for (int i = 1; i <= m; ++i) {
        in >> x >> y;
        graf[x].push_back(y);
    }
}

int main() {
    citire();

    for (int i = 1; i <= n; ++i)
        if (!vizitat[i])
            dfs(i);
    return 0;
}