Cod sursa(job #2666428)

Utilizator Mari_BarbuBarbu Mariana Mari_Barbu Data 1 noiembrie 2020 20:05:42
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

ifstream f("sortaret.in");
ofstream g("sortaret.out");

vector<int> v[100002];
int viz[100002];
int n, m;

void dfs(int x)
{
    viz[x] = 1;
    for (auto &i : v[x])
    {
        if (viz[i] == 0)
            dfs(i);
    }
    g << x << ' ';
}
int main()
{
    f >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int x, y;
        f >> x >> y;
        v[y].push_back(x);
    }
    for (int i = 1; i <= n; i++)
    {
        if (viz[i] == 0)
        {
            dfs(i);
        }
    }
}