Pagini recente » Cod sursa (job #616263) | Cod sursa (job #720649) | Cod sursa (job #1306961) | Cod sursa (job #792185) | Cod sursa (job #2539970)
#include <bits/stdc++.h>
std::ifstream fin("sortaret.in");
std::ofstream fout("sortaret.out");
int n, m;
int weigth[50005], sol[50005];
std::vector<int> graph[50005];
std::queue<int> q;
int main()
{
fin>>n>>m;
while(m--){
int x, y;
fin>>x>>y;
graph[x].push_back(y);
++weigth[y];
}
for(int i=1; i<=n; ++i) if(!weigth[i]) q.push(i);
n=0;
while(!q.empty()){
int now=q.front(); q.pop();
sol[++n]=now;
for(auto next:graph[now]){
--weigth[next];
if(!weigth[next]) q.push(next);
}
}
for(int i=1; i<=n; ++i) fout<<sol[i]<<" ";
return 0;
}