Pagini recente » Cod sursa (job #334135) | Cod sursa (job #742276) | Cod sursa (job #2534618) | Cod sursa (job #2577921) | Cod sursa (job #2415670)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50005;
vector<int> graf[MAXN];
int grad[MAXN];
queue<int> q;
vector<int> ans;
int main()
{
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m;
fin >> n >> m;
while(m){
int x, y;
fin >> x >> y;
grad[y] = grad[x] + 1;
graf[x].push_back(y);
m--;
}
for(int i = 1; i <= n; ++i){
if(grad[i] == 0){
ans.push_back(i);
q.push(i);
}
}
while(!q.empty()){
int nod = q.front();
q.pop();
for(int i = 0; i < int(graf[nod].size()); ++i){
grad[graf[nod][i]]--;
if(grad[graf[nod][i]] == 0){
ans.push_back(graf[nod][i]);
q.push(graf[nod][i]);
}
}
}
for(int i = 0; i < int(ans.size()); ++i)
fout << ans[i] << " ";
return 0;
}