Cod sursa(job #1701190)

Utilizator c0mradec0mrade c0mrade Data 12 mai 2016 13:36:01
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
int n,m,viz[50010];
vector<int> G[50010];
stack<int> stk;

void sort_top(int x)
{
    viz[x] = 1;
    for(int i = 0;i < G[x].size();++i)
        if(!viz[G[x][i]])
            sort_top(G[x][i]);
    stk.push(x);
}

int main()
{
    in>>n>>m;
    for (int i = 1;i <= m;++i)
    {
        int x, y;
        in >> x >> y;
        G[x].push_back(y);
    }
    for(int i = 1;i <= n;++i)
        if(!viz[i])
            sort_top(i);
    while(stk.size()){
        out<<stk.top()<<" ";
        stk.pop();
    }
    return 0;
}