Pagini recente » Cod sursa (job #3200733) | Cod sursa (job #3592) | Clasament prosoft2017-9 | Cod sursa (job #2201089) | Cod sursa (job #3288567)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int Max = 5e4 + 1;
int n, m;
vector<int> graph[Max], d(Max), ans;
queue<int> q;
void kahn()
{
for(int i = 1; i <= n; ++i)
if(!d[i])
q.push(i);
while(!q.empty())
{
int node = q.front();
q.pop();
ans.push_back(node);
for(int i: graph[node])
if(!--d[i])
q.push(i);
}
}
int main()
{
ios::sync_with_stdio(false);
fin.tie(nullptr);
fin >> n >> m;
for(int i = 1; i <= m; ++i)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
++d[y];
}
kahn();
for(int i: ans)
fout << i << " ";
fin.close();
fout.close();
return 0;
}