Răspuns:
#include <bits/stdc++.h>
#define DIM 100010
using namespace std;
ifstream fin("topsort.in");
ofstream fout("topsort.out");
int n,m,i,x,y,k,r,t[DIM],sol[DIM];
vector<int> L[DIM];
bitset<DIM> f;
void dfs(int nod) {
f[nod]=1;
for (int i=0;i<L[nod].size();i++) {
int vecin=L[nod][i];
if (f[vecin]==0)
dfs(vecin);
}
sol[k++]=nod;
}
int main() {
fin>>n>>m;
for (i=1;i<=m;i++) {
fin>>x>>y;
L[x].push_back(y);
t[y]=x;
}
for (i=1;i<=n;i++)
if (f[i]==0)
dfs(i);
while (k--)
fout<<sol[k]<<" ";
return 0;
}
Explicație: