Pagini recente » Cod sursa (job #2402721) | Cod sursa (job #682352) | Cod sursa (job #2841239) | Cod sursa (job #2080191) | Cod sursa (job #688512)
Cod sursa(job #688512)
#include<stdio.h>
#include<assert.h>
#include<vector>
using namespace std;
const int kvert=50005;
vector<int> sorted, queue, graph[kvert];
int n, m, no_iedges[kvert];
void read(){
assert(freopen("sortaret.in","r",stdin)!=NULL);
scanf("%d%d",&n ,&m);
int i, x, y;
for(i = 1; i <= m; ++i){
scanf("%d%d",&x ,&y);
++no_iedges[y];
graph[x].push_back(y);
}
}
void insert(int x){
sorted.push_back(x);
int i;
for(i = 0; i < graph[x].size(); ++i){
--no_iedges[graph[x][i]];
if(no_iedges[graph[x][i]] == 0)
queue.push_back(graph[x][i]);
}
}
void solve(){
int i;
for(i = 1; i <= n; ++i)
if(no_iedges[i] == 0)
queue.push_back(i);
int curent = 0;
while(curent < queue.size()){
insert(queue[curent]);
++curent;
}
}
void write(){
assert(freopen("sortaret.out","w",stdout)!=NULL);
if(sorted.size() < n)
printf("-1");
else
for(int i = 0; i < sorted.size(); ++i)
printf("%d ",sorted[i]);
}
int main(){
read();
solve();
write();
return 0;
}