Pagini recente » Cod sursa (job #2410712) | Cod sursa (job #40206) | Cod sursa (job #703430) | Cod sursa (job #23356) | Cod sursa (job #754978)
Cod sursa(job #754978)
#include <stdio.h>
#include <stdlib.h>
struct LI {
int t;
struct LI *urm;
};
struct LI *v, l;
int *ins;
FILE* fo;
void LI_ad( struct LI *d, int element ) {
struct LI *b;
b = (struct LI*) malloc( sizeof(struct LI) );
b->t = element;
b->urm = d->urm;
d->urm = b;
}
void berak_rek( struct LI *d, int idx ) {
if ( ins[idx] )
return;
ins[idx] = 1;
LI_ad( d, idx );
struct LI *p;
p = v[idx].urm;
while (p != NULL) {
berak_rek( d->urm, p->t );
p = p->urm;
}
}
int main(void) {
int n,m,i,x,y;
l.urm = NULL;
FILE * f;
f = fopen("sortaret.in", "r");
fscanf(f,"%d %d", &n, &m);
v = (struct LI *) calloc( (n+1), sizeof( struct LI ) );
ins = (int*) calloc( (n+1), sizeof( int ) );
for ( i = 0; i<m; i++) {
fscanf(f,"%d %d", &x, &y);
LI_ad( &v[y], x );
}
fclose(f);
for (i = 1; i<=n; i++ )
berak_rek( &l, i );
fo = fopen("sortaret.out", "w");
x=1;
for (i=1; i<=n; i++) {
if (ins[i] == 0)
ins[x++] = i;
}
i = n;
struct LI* p;
p = l.urm;
while (p!=NULL) {
ins[i--] = p->t;
p = p->urm;
}
for (i=1;i<=n;i++)
fprintf(fo,"%d ",ins[i]);
fclose(fo);
return 0;
}