Pagini recente » Cod sursa (job #2161327) | Cod sursa (job #3291678) | Cod sursa (job #3187162) | Cod sursa (job #670899) | Cod sursa (job #175013)
Cod sursa(job #175013)
#include<fstream.h>
#include<stdlib.h>
#define dim 5000//1
ifstream f("sortaret.in");
ofstream g("sortaret.out");
unsigned long n,m,nn;
unsigned long *a[dim];
unsigned long v[dim];
char viz[dim];
void dfs(unsigned long vf)
{unsigned long c;
nn++;
v[nn]=vf;
viz[vf]=1;
for(c=1;c<=a[vf][0];c++)
if(!viz[a[vf][c]]) dfs(a[vf][c]);
}
int main()
{unsigned long i,j,x,y;
f>>n>>m;
for(i=1;i<=n;i++)
{a[i]=(unsigned long*)realloc(a[i],sizeof(unsigned long));
a[i][0]=0;
}
for(i=1;i<=m;i++)
{f>>x>>y;
a[x][0]++;
a[x]=(unsigned long*)realloc(a[x],a[x][0]*sizeof(unsigned long));
a[x][a[x][0]]=y;
}
f.close();
for(i=1;i<=n;i++)
if(!viz[i]) dfs(i);
for(i=1;i<=n;i++) g<<v[i]<<" ";
g.close();
return 0;
}