Cod sursa(job #1575660)

Utilizator tudormaximTudor Maxim tudormaxim Data 21 ianuarie 2016 18:19:02
Problema Sortare topologica Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax = 50005;
vector <int> g[nmax];
bool viz[nmax];
int grint[nmax];

int main()
{
    ifstream fin ("sortaret.in");
    ofstream fout ("sortaret.out");
    ios_base::sync_with_stdio(false);
    int n, m, i, j, x, y;
    bool found=false;
    fin >> n >> m;
    for(i=1; i<=m; i++)
    {
        fin >> x >> y;
        grint[y]++;
        g[x].push_back(y);
    }
    for(i=1; i<=n; i++)
    {
        found=false;
        for(j=1; j<=n && !found; j++)
            if(grint[j]==0 && !viz[j])
            {
                viz[j]=true;
                found=true;
                x=j;
            }
        fout << x << " ";
        for(j=0; j<g[x].size(); j++)
            grint[g[x][j]]--;
    }
    fin.close();
    fout.close();
    return 0;
}