Cod sursa(job #2361352)

Utilizator qwerty1234qwerty qwerty1234 Data 2 martie 2019 14:53:53
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Nmax = 50005;

vector < int > L[Nmax];

int ans[Nmax] , n , d;

bool viz[Nmax];

void DFS(int nod)
{
    viz[nod] = true;
    for(auto it : L[nod])
        if(!viz[it])
            DFS(it);
    ans[++d] = nod;
}


int main()
{
    int x , y;
    fin >> n;
    for(int i = 1 ; i <= n ; i++)
        fin >> x >> y , L[x].push_back(y) , L[y].push_back(x);
    for(int i = 1 ; i <= n ; i++)
        if(!viz[i])
            DFS(i);
    for(int i = d ; i >= 1 ; i--)
        fout << ans[i] << " ";
    fout << "\n";
    fin.close();
    fout.close();
    return 0;
}