Cod sursa(job #2722880)

Utilizator sichetpaulSichet Paul sichetpaul Data 13 martie 2021 12:49:35
Problema Sortare topologica Scor 0
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], ans;

void DFS(int node) {
    vis[node] = 1;
    for (auto it: G[node])
        if (!vis[it]) DFS(it);
    ans.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] == 0) DFS(i);

    reverse(ans.begin(), ans.end());
    for (auto it: ans)
        cout << it << " ";

    return 0;
}