Cod sursa(job #2797250)

Utilizator Rares5000Baciu Rares Rares5000 Data 9 noiembrie 2021 17:08:08
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
#include <vector>
#define UNTIE ios::sync_with_stdio(0); fin.tie(0);

using namespace std;

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

vector<int> v[100002];
int vis[100002], sol[100002], len, n, m;

void DFS(int x)
{
    vis[x] = 1;
    for(auto w : v[x])
        if(vis[w] == 0)
            DFS(w);
    sol[++len] = x;
}

int main()
{
    int i, j, x, y;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y;
        v[x].push_back(y);
    }
    for(i = 1; i <= n; i++)
        if(vis[i] == 0)
            DFS(i);
    for(i = len; i >= 1; i--)
        fout << sol[i] << " ";
    fin.close();
    fout.close();
    return 0;
}