Cod sursa(job #1565779)

Utilizator Narcys01Ciovnicu Narcis Narcys01 Data 11 ianuarie 2016 12:46:12
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

int n, m;
vector <int> a[50004];
int g[50004], q[50004];

int main()
{
    int i, x, y, l, j;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        a[x].push_back(y);
        g[y]++;
    }
    l = 0;
    for (i = 1; i <= n; i++)
        if (g[i] == 0)
            q[++l] = i;
    for (i = 1; i <= n; i++)
        cout << g[i] << " ";
    for (i = 1; i <= n; i++)
    {
        x = q[i];
        int k = a[x].size();
        for (j = 0; j < k; j++)
        {
            g[a[x][j]]--;
            if (g[a[x][j]] == 0)
                q[++l] = a[x][j];
        }
    }
    for (i = 1; i <= l; i++)
        fout << q[i] << " ";
    return 0;
}