Pagini recente » Cod sursa (job #1078328) | Cod sursa (job #249256) | Cod sursa (job #670174) | Cod sursa (job #533753) | Cod sursa (job #150781)
Cod sursa(job #150781)
#include<fstream.h>
using namespace std;
#define nmax 50100
#define mmax 100100
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int n, m, a[mmax], st[nmax], poz[nmax], viz[nmax], nr, sol[nmax];
int cmp(int x, int y)
{
if( viz[x] > viz[y] ) return 0;
else return 1;
}
void citire()
{
int i, x, y, sum;
f>>n>>m;
for(i=1; i<=m; i++){
f>>x>>y;
st[x]++;
}
f.close();
sum=0;
for(i=1; i<=n+2; i++){
st[i-1]=sum;
sum=sum+st[i];
}
ifstream f("sortaret.in");
f>>n>>m;
for(i=1; i<=m; i++){
f>>x>>y;
a[st[x]]=y;
st[x]--;
}
f.close();
}
void DFS(int x)
{
int i;
for(i=st[x]+1; i<=st[x+1]; i++)
if(!viz[a[i]]){
viz[a[i]]=viz[x]+1;
DFS(a[i]);
}
}
int main()
{
int i, j;
citire();
for(i=1; i<=m; i++) sol[a[i]]++;
for(i=1; i<=n; i++) if(!sol[i]) DFS(i);
for(i=1; i<=n; i++) poz[i]=i;
sort( poz+1, poz+n+1, cmp);
for(i=1; i<=n; i++) g<<poz[i]<<' ';
g.close();
return 0;
}