Cod sursa(job #1130726)

Utilizator EhtRalpmetFMI Ardei Claudiu-Alexandru EhtRalpmet Data 28 februarie 2014 15:14:29
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
//#include <iostream>
#include<fstream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
ifstream cin("sortaret.in");
ofstream cout("sortaret.out");
#define maxn 500001

int a,b,m,n,grad[maxn];
vector<int> g,v[maxn];

void topsort(){
    for(int i=1;i<=n;i++){
        if(grad[i]==0){
            g.push_back(i);
        }
    }
    for(int i=0;i<g.size();i++){
        for(int j=0;j<v[g[i]].size();j++){
            grad[v[g[i]][j]]--;
            if(grad[v[g[i]][j]]==0){
                g.push_back(v[g[i]][j]);
            }
        }
    }
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>a>>b;
        v[a].push_back(b);
        grad[b]++;
    }
    topsort();
    for(int i=0;i<g.size();i++){
        cout<<g[i]<<'\n';
    }
    return 0;
}