Cod sursa(job #2651521)

Utilizator robeert.77Chirica Robert robeert.77 Data 22 septembrie 2020 20:19:07
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
#include <vector>
using namespace std;

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

int main() {
    int n, m;
    fin >> n >> m;
    vector<int> nodesSorted;
    bool reached[n + 1] = {0};
    int x, y;
    for (int i = 0; i < n; i++) {
        fin >> x >> y;
        if (!reached[x])
            nodesSorted.push_back(x);
        if (!reached[y])
            nodesSorted.push_back(y);
        reached[x] = true;
        reached[y] = true;
    }

    for (vector<int>::iterator it = nodesSorted.begin(); it != nodesSorted.end(); it++)
        fout << *it << " ";
    for (int i = 1; i <= n; i++)
        if (!reached[i])
            fout << i << " ";
    return 0;
}