Cod sursa(job #2738614)

Utilizator sichetpaulSichet Paul sichetpaul Data 6 aprilie 2021 09:43:35
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
#define Nmax 50005
using namespace std;

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

int N, M;
bool in[Nmax], vis[Nmax];
vector<int> G[Nmax], nodes;

void DFS(int node) {
   vis[node] = 1;
   for (auto it: G[node])
       if (!vis[it]) DFS(it);
   nodes.push_back(node);
}
int main()
{
    fin >> N >> M;
    while (M--) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
        in[y] = 1;
    }
    for (int i = 1; i <= N; ++i)
        if (!in[i]) DFS(i);

    reverse(nodes.begin(), nodes.end());
    for (auto it: nodes)
        fout << it << " ";

    return 0;
}