Cod sursa(job #3213417)

Utilizator tomaionutIDorando tomaionut Data 13 martie 2024 09:30:29
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>

using namespace std;

int n, m, st[50005], viz[50005], top;
vector <int> a[50005];

void Dfs(int x)
{
    viz[x] = 1;
    for (int w : a[x])
        if (viz[w] == 0)
        Dfs(w);
    st[++top] = x;
}

int main()
{
    int i, x, y;
    ifstream cin("sortaret.in");
    ofstream cout("sortaret.out");
    cin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        cin >> x >> y;
        a[x].push_back(y);
      //  a[y].push_back(x);
    }

    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
        Dfs(i);

    for (i = n; i >= 1; i--)
        cout << st[i] << " ";

    return 0;
}