Cod sursa(job #2277452)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 6 noiembrie 2018 11:55:47
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX=5e4+5;

vector <int> G[NMAX], A;

int N, M, i, X, Y;

bool Sel[NMAX];

void DFS (int X)
{
    A.push_back(X);

    Sel[X]=true;

    for(int i=0; i<G[X].size(); i++)
        if(!Sel[G[X][i]])
            DFS(G[X][i]);
}

int main()
{
    f>>N>>M;

    for(i=1; i<=M; i++)
    {
        f>>X>>Y;

        G[X].push_back(Y);
    }

    for(i=1; i<=N; i++)
        sort(G[i].begin(), G[i].end());

    for(i=1; i<=N; i++)
        if(!Sel[i])
            DFS(i);

    for(i=0; i<A.size(); i++)
        g<<A[i]<<' ';

    g<<'\n';

    return 0;
}