Cod sursa(job #1760281)

Utilizator oldboyOld Boy oldboy Data 20 septembrie 2016 17:12:58
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
#define Nmax 50005

using namespace std;

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

vector <int> L[Nmax];
int n, m, v[Nmax], k;
bool viz[Nmax];

void DFS(int nod)
{
    viz[nod] = true;
    for(unsigned it = 0; it < L[nod].size();  it++)
        if(!viz[it])
            DFS(it);
    v[++k] = nod;
}

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

void Rezolv()
{
    int i;
    for(i = 1; i <= n; i++)
        if(!viz[i])
            DFS(i);
    for(i = k; i > 1; i--)
        g << v[i] << " ";
    g.close();
}

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