Cod sursa(job #1879793)

Utilizator ifrimencoAlexandru Ifrimenco ifrimenco Data 15 februarie 2017 10:13:31
Problema Sortare topologica Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;
int viz[60001];
set <int> v[60001];
vector <int> postordine;
void DFS(int x)
{
    viz[x]=1;
    set <int>::iterator it;
    for (it=v[x].begin();it!=v[x].end();++it)
        if (!viz[*it])
        DFS(*it);
        postordine.push_back(x);
}
int main()
{
    ifstream f("sortaret.in");
    ofstream g("sortaret.out");
    int i, j, n, m;
    f >> n  >> m;
    for (i=1;i<=m;++i)
    {
        f >> i >> j;
        v[i].insert(j);

    }
    for (i=1;i<=n;++i)
        if (!viz[i]) DFS(i);
   vector <int>:: reverse_iterator it;
   while (!postordine.empty())
   {
       g << postordine.back() << " ";
       postordine.pop_back();
   }

    return 0;
}