Cod sursa(job #2651717)

Utilizator ArkhamKnightyMarcus ArkhamKnighty Data 23 septembrie 2020 13:15:12
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 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("sortaret.in");
ofstream cout("sortaret.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;
}