Pagini recente » Cod sursa (job #2113439) | Cod sursa (job #590379) | Cod sursa (job #1129100) | Cod sursa (job #2508057) | Cod sursa (job #2526167)
#include <fstream>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int N=50001;
const int M=2*N;
int n,m, lst[N], vf[2*M], urm[2*M], sorttop[N], nr,s, ns;
bool viz[N];
void adauga(int x, int y)
{
vf[++nr]=y;
urm[nr]=lst[x];
lst[x]=nr;
}
void dfs(int x)
{
viz[x]=true;
int y;
for(int p=lst[x]; p!=0; p=urm[p])
{
y=vf[p];
if(!viz[y])
dfs(y);
}
sorttop[++ns]=x;
}
int main()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
{
int x, y;
fin>>x>>y;
adauga(x,y);
}
dfs(1);
for(int i=ns; i>=1; i--)
fout<<sorttop[i]<<" ";
return 0;
}