Pagini recente » Cod sursa (job #2287231) | Cod sursa (job #2068482) | Cod sursa (job #1728192) | Cod sursa (job #914044) | Cod sursa (job #475074)
Cod sursa(job #475074)
#include<stdio.h>
struct Cell{
int val;
Cell *next;
};
Cell **a;
int *b, *c, *viz, index;
int n, m;
void df(int x){
viz[x] = 1;
for(Cell *cell = a[x]; cell; cell = cell->next)
if(!viz[cell->val]){
df(cell->val);
}
c[index++] = x;
}
void sort(){
for(int i=1;i<=n;i++)
if(!b[i]) df(i);
}
int main()
{
FILE *ofile, *ifile;
ifile = fopen("sortaret.in", "r");
fscanf(ifile, "%i %i", &n, &m);
a = new Cell*[n+1];
for(int i=0; i<=n; i++)
a[i] = NULL;
b = new int[n+1];
c = new int[n];
viz = new int[n+1];
for(int i=0;i<=n;i++)
b[i] = viz[i] = 0;
for(int i=0; i<m; i++){
Cell *cell = new Cell;
int x;
fscanf(ifile, "%i %i", &x, &cell->val);
cell->next = a[x];
a[x] = cell;
b[cell->val] = 1;
}
fclose(ifile);
sort();
ofile = fopen("sortaret.out", "w");
for(int i=index-1;i>=0;i--)
fprintf(ofile, "%i ", c[i]);
fclose(ofile);
return 0;
}