Pagini recente » Cod sursa (job #1687753) | Cod sursa (job #2823805) | Cod sursa (job #232711) | Cod sursa (job #1245847) | Cod sursa (job #2163246)
#include <fstream>
#include <vector>
#define nmax 50010
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
vector <int> v[nmax];
bool use[nmax];
int n,m,x,y,i,nr,postordine[nmax];
void topsort(int x)
{
use[x]=1;
for (int i=0; i<v[x].size(); i++)
if (!use[v[x][i]])
topsort(v[x][i]);
postordine[++nr]=x;
}
int main()
{
fin >> n >> m;
for (i=1; i<=m; i++)
{
fin >> x >> y;
v[x].push_back(y);
}
for (i=1; i<=n; i++)
if (!use[i])
topsort(i);
for (i=nr; i; i--)
fout << postordine[i] << ' ';
fout << '\n';
}