Cod sursa(job #2667389)

Utilizator marianeacsuNeacsu Maria marianeacsu Data 3 noiembrie 2020 13:26:46
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb

#include <fstream>
#include <vector>
#define N 100005

using namespace std;

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

int n, m, viz[N], S[N], top;
vector <int> muchii[N];

void Citire()
{
    int i, x, y;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        muchii[x].push_back(y);
    }
}


void DFS(int x)
{
    viz[x] = 1;
    for (auto i : muchii[x])
    {
        if (viz[i] == 0)
            DFS(i);
    }
    S[++top] = x;
}

void TopSort()
{
    int i;
    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
            DFS(i);
    while (top > 0)
    {
        fout << S[top] << " ";
        top--;
    }
}


int main()
{
    Citire();
    TopSort();
    return 0;
}