Pagini recente » Cod sursa (job #690304) | Cod sursa (job #704644) | Cod sursa (job #1494397) | Cod sursa (job #921157) | Cod sursa (job #2735750)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m;
vector<int> v[50005];
int grad[50005];
int st[50005], top;
int answer[50005], cnt;
int main()
{
int x, y;
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> x >> y;
v[x].push_back(y);
grad[y]++;
}
for(int i = 1; i <= n; i++)
if(grad[i] == 0)
st[++top] = i;
while(top)
{
x = st[top--];
answer[++cnt] = x;
for(auto nod: v[x])
{
grad[nod]--;
if(grad[nod] == 0)
st[++top] = nod;
}
}
for(int i = 1; i <= n; i++)
{
fout << answer[i] << " ";
}
fout << "\n";
return 0;
}