Pagini recente » Cod sursa (job #2927216) | Cod sursa (job #1230953) | Cod sursa (job #1917574) | Cod sursa (job #1243251) | Cod sursa (job #1746816)
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100009;
vector < int > g[nmax];
int in[nmax] , arr[nmax] , topo[nmax];
int n , m , x , y , i , j , top;
int main()
{
freopen("sortaret.in" , "r" , stdin);
freopen("sortaret.out" , "w" , stdout);
scanf("%d" , &n);
scanf("%d" , &m);
for (i = 1 ; i <= m ; ++i)
{
scanf("%d" , &x);
scanf("%d" , &y);
g[x].push_back(y);
in[y]++;
}
for (i = 1 ; i <= n ; ++i)
g[0].push_back(i) , in[i]++;
arr[++top] = 0;
for (i = 0 ; i <= n ; ++i)
{
x = arr[top] , top--;
topo[i] = x;
for (j = 0 ; j < g[x].size() ; ++j)
{
y = g[x][j];
in[y]--;
if (in[y] == 0) arr[++top] = y;
}
}
for (i = 1 ; i <= n ; ++i)
printf("%d " , topo[i]);
return 0;
}