Pagini recente » Cod sursa (job #265138) | Cod sursa (job #1963457) | Cod sursa (job #3247023) | Cod sursa (job #464316) | Cod sursa (job #759088)
Cod sursa(job #759088)
#include <stdio.h>
struct nod {
nod *next;
int x;
};
struct lista {
nod *first, *last;
lista() {
first = NULL;
last = NULL;
}
void add(int x) {
nod *nou = new nod;
nou->x = x;
nou->next = NULL;
if(last != NULL) last->next = nou;
last = nou;
if(first == NULL) first = last;
}
};
int main() {
FILE *f = fopen("sortaret.in", "r"), *g = fopen("sortaret.out", "w");
int n, m;
fscanf(f, "%d %d", &n, &m);
lista *liste = new lista[n];
lista st;
int *grad_extern = new int[n];
for(int i = 0; i < n; i++) grad_extern[i] = 0;
for(int i = 0; i < m; i++) {
int k, l;
fscanf(f, "%d %d", &k, &l);
liste[k - 1].add(l - 1);
grad_extern[l - 1] ++;
}
for(int i = 0; i < n; i++)
if(grad_extern[i] == 0) {
st.add(i);
fprintf(g, "%d ", i + 1);
}
nod *contor = st.first;
while(contor != NULL) {
nod *c2 = liste[contor->x].first;
if(grad_extern[contor->x] == 0)
while(c2 != NULL) {
st.add(c2->x);
fprintf(g, "%d ", c2->x + 1);
grad_extern[c2->x]--;
c2 = c2->next;
}
else {
break;
}
contor = contor->next;
}
fclose(f);
fclose(g);
return 0;
}