Cod sursa(job #1738971)

Utilizator tudorgalatanRoman Tudor tudorgalatan Data 8 august 2016 12:20:40
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
#include <vector>

using namespace std;

void DFSsort (unsigned short int node);

unsigned short int N;
unsigned int M;
unsigned short int X, Y;

vector <unsigned  int> G[50001];
stack <unsigned short int> nodes;
unsigned int degree[50001];
bool seen[50001];
unsigned int i;

int main ()
{
    ifstream fin ("sortaret.in");
    fin >> N >> M;
    for (i=1; i<=M; i++)
    {
        fin >> X >> Y;
        G[X].push_back(Y);
        degree[Y]++;
    }
    fin.close();
    for (i=1; i<=n; i++)
        if (!seen[i])
            DFSsort(i);
    ofstream fout ("sortaret.out");
    while (!nodes.empty())
    {
        fout << nodes.top() << ' ';
        nodes.pop();
    }
    fout.close();
    return 0;
}

void DFSsort (unsigned short int node)
{
    vector <unsigned int> :: iterator i;
    seen[node] = 1;
    for (i=G[node].begin(); i!=G[node].end(); i++)
        if (!seen[*i])
            DFSsort(*i);
    nodes.push(node);
}