Pagini recente » Cod sursa (job #745906) | Cod sursa (job #1664716) | Cod sursa (job #1855835) | Cod sursa (job #2091674) | Cod sursa (job #1653770)
#include <fstream>
#include <stdlib.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int *A[50005], i, n, m, x, y;
int Stiva[50005], nr, Valid[50005];
void DFS(int x)
{
int i, p;
Valid[x]=1;
for (i=1; i<=A[x][0]; i++)
{
p=A[x][i];
if (Valid[p]==0)
DFS(p);
}
Stiva[++nr]=x;
}
int main()
{
fin>>n>>m;
for (i=1; i<=n; i++)
{
A[i] = (int *) realloc(A[i], sizeof(int));
A[i][0]=0;
}
for (i=1; i<=m; i++)
{
fin>>x>>y;
A[x][0]++;
A[x] = (int *) realloc(A[x],(A[x][0]+1)*sizeof(int));
A[x][A[x][0]]=y;
}
for (i=1; i<=n; i++)
{
if (Valid[i]==0)
{
DFS(i);
}
}
for (i=n; i>=1; i--)
fout<<Stiva[i]<<" ";
return 0;
}