Pagini recente » Cod sursa (job #1981142) | Profil BlackNesta | Cod sursa (job #1354482) | Cod sursa (job #1507531) | Cod sursa (job #2529470)
#include <iostream>
#include <fstream>
using namespace std;
const int N = 50000, M = 100000;
int n, vf[M + 1], urm[M + 1], lst[M + 1], nr, cnt[N + 1], q[N + 1], st, dr = -1;
void adaug(int x, int y){
vf[++nr] = y;
urm[nr] = lst[x];
lst[x] = nr;
cnt[y]++;
}
void citire(){
ifstream in("sortaret.in");
int m;
in >> n >> m;
for(int i = 0; i < m; i++){
int x, y;
in >> x >> y;
adaug(x, y);
}
}
void sortare(){
for(int i = 1; i <= n; i++){
if(cnt[i] == 0){
q[++dr] = i;
}
}
while(st <= dr){
int x = q[st++];
//out << x << ' ';
for(int p = lst[x]; p != 0; p = urm[p]){
int y = vf[p];
cnt[y]--;
if(cnt[y] == 0){
q[++dr] = y;
}
}
}
}
int main(){
citire();
sortare();
ofstream out("sortaret.out");
for(int i = 0; i <= dr; i++){
out << q[i] << ' ';
}
out.close();
return 0;
}