Cod sursa(job #2651564)

Utilizator ArkhamKnightyMarcus ArkhamKnighty Data 22 septembrie 2020 22:51:18
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <algorithm>
#include <vector>


using namespace std;

const int INF = 2e9;
const int N = 5e4;

int n, m;

vector < vector <int> > G;
vector <int> sol;
bool viz[5 + N];

ifstream cin("sate.in");
ofstream cout("sate.out");

void DFS(int node)
{
    viz[node] = true;
    for(int i = 0 ; i < G[node].size() ; i++)
        if(!viz[ G[node][i] ])
            DFS(G[node][i] );

    sol.push_back(node);
}

int main()
{
    int x, y;

    cin >> n >> m;

    G.resize(n+ 5);
    for(int i = 1; i <= m; i++)
        cin >> x >> y,
        G[x].push_back(y);


    for(int i = 1; i <= n; i++)
        if(!viz[i])
            DFS(i);


    reverse(sol.begin(), sol.end());

    for(int i = 0 ; i < sol.size() ; i++)
        cout << sol[i] << ' ';
    return 0;
}