Cod sursa(job #872158)

Utilizator mihai27Mihai Popescu mihai27 Data 5 februarie 2013 20:44:48
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include<fstream>
#include<vector>

using namespace std;

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

int n,m,i,x,y,ok[50001];
vector<int> a[50001],Q;

void DF(int nod)
{
    ok[nod]=1;
    for (vector<int>::iterator it=a[nod].begin();it!=a[nod].end();it++)
        if (!ok[*it]) DF(*it);
    Q.push_back(nod);
}

int main()
{
    in>>n>>m;
    for (i=1;i<=m;i++)
    {
        in>>x>>y;
        a[x].push_back(y);
    }
    for (i=1;i<=n;i++)
        if (!ok[i]) DF(i);

    for (i=Q.size()-1;i>=0;i--)
        out<<Q[i]<<' ';
}