Cod sursa(job #2961040)

Utilizator ionanghelinaIonut Anghelina ionanghelina Data 5 ianuarie 2023 16:44:23
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include<bits/stdc++.h>
using namespace std;

vector<int> v[50005];

bool seen[50005];

vector<int> solution;

void dfs(int nod)
{
    seen[nod] = 1;


    for(auto it:v[nod])
    {
        if(seen[it]) continue;
        dfs(it);
    }

    solution.push_back(nod);
}
int main()
{

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

    int n,m;


    fin>>n>>m;

    for(int i=1;i<=m;i++)
    {
        int x,y;
        fin>>x>>y;

        v[x].push_back(y);
    }

    for(int i=1;i<=n;i++)
    {
        if(!seen[i])
            dfs(i);
    }

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

    for(auto it:solution)
        fout<<it<<' ';
    fout<<'\n';

    return 0;
}