Pagini recente » Cod sursa (job #14397) | Cod sursa (job #3291458) | Cod sursa (job #169755) | Cod sursa (job #2973454) | Cod sursa (job #168787)
Cod sursa(job #168787)
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <vector.h>
#define MAXN 50100
int n, in[MAXN],sol[MAXN],r;
vector<int> mat[MAXN];
int main(void){
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int m,x,y;
fin >> n;
for (fin >> m;m;m--){
fin >> x >> y;
mat[x].push_back(y);
in[y]++;
}
for (int i=1;i<=n;i++)
if (!in[i]) sol[r++] = i;
int i = 0;
while (i != r){
in[sol[i]] = -1;
fout << sol[i] << " ";
for (vector<int>::iterator it = mat[sol[i]].begin();
it != mat[sol[i]].end(); ++it) {
in[*it]--;
if (!in[*it])
sol[r++] = *it;
}
i++;
}
fin.close();
fout.close();
return 0;
}