Cod sursa(job #2671132)

Utilizator mihai_radulescuMihai Radulescu mihai_radulescu Data 11 noiembrie 2020 15:22:44
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 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);
    }
}