Cod sursa(job #2594753)

Utilizator maria_mMaria Mosneag maria_m Data 6 aprilie 2020 16:28:52
Problema Sortare topologica Scor 90
Compilator c-64 Status done
Runda Arhiva educationala Marime 3.48 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NMAX 50000

struct Node {
    int data;
    struct Node *next;
};

typedef struct {
    struct Node *head;
    struct Node *tail;
    int size;
} LinkedList;

typedef struct {
    LinkedList **neighbors;
    int nodes;
} ListGraph;

void init_list(LinkedList *list) {
    list->head = NULL;
    list->tail = NULL;
    list->size = 0;
}

void init_list_graph(ListGraph *graph, int nodes) {
    graph->nodes = nodes;
    graph->neighbors = malloc(nodes * sizeof(LinkedList));

    if (graph->neighbors == NULL) {
        perror("Not enough memory to initialize the adjacency list!");
        exit(-1);
    }

    for (int i = 0; i < nodes; ++i) {
        graph->neighbors[i] = malloc(sizeof(LinkedList));

        if (graph->neighbors[i] == NULL) {
            perror("Not enough memory to initialize the adjacency list!");
            exit(-1);
        }
        init_list(graph->neighbors[i]);
    }
}

void add_nth_node(LinkedList *list, int n, int new_data) {
    struct Node *prev, *curr;
    struct Node *new_node;

    if (list == NULL) {
        return;
    }

    if (n > list->size) {
        n = list->size;
    } else if (n < 0) {
        return;
    }

    curr = list->head;
    prev = NULL;
    while (n > 0) {
        prev = curr;
        curr = curr->next;
        --n;
    }

    new_node = malloc(sizeof(struct Node));
    if (new_node == NULL) {
        perror("Not enough memory to add element!");
        exit(-1);
    }
    
    new_node->data = new_data;
    new_node->next = curr;
    if (prev == NULL) {
        list->head = new_node;
    } else {
        prev->next = new_node;
    }

    if (new_node->next == NULL) {
        list->tail = new_node;
    }

    list->size++;
}

void add_edge_list_graph(ListGraph *graph, int src, int dest) {
	struct Node *p;
	for (p = graph->neighbors[src]->head; p; p = p->next) {
		if (p->data == dest) {
			return;
		}
	}
    add_nth_node(graph->neighbors[src], (1 <<30), dest);
}

void dfs_topo_sort(ListGraph *lg, int node, int *visited, int *stack, int *n) {
	// printf("%d\n", node);
	struct Node *p;
	visited[node] = 1;
	for (p = lg->neighbors[node]->head; p; p = p->next) {
		if (visited[p->data] == 0) {
			dfs_topo_sort(lg, p->data, visited, stack, n);
		}
	}
	stack[*n] = node;
	// printf("added to stack on %d: %d\n", *n, node);
	*n = *n + 1;
}

void topo_sort(ListGraph *lg, FILE *out) {
    int i, viz[NMAX], n = 0, stack[NMAX];
    memset(viz, 0, NMAX);
    for (i = 1; i < lg->nodes; ++i) {
    	if (viz[i] == 0) {
    		dfs_topo_sort(lg, i, viz, stack, &n);
    	}
    }
    for (i = lg->nodes - 2; i >= 0; i--) {
    	fprintf(out, "%d ", stack[i]);
    }
}

void print_int_linkedlist(LinkedList *list) {
    struct Node *curr;

    if (list == NULL) {
        return;
    }

    curr = list->head;
    while (curr != NULL) {
        printf("%d ", curr->data);
        curr = curr->next;
    }

    printf("\n");
}

void print_list_graph(ListGraph *g) {
    int i;
    for (i = 1; i <= g->nodes; i++) {
        printf("%d: ", i);
        print_int_linkedlist(g->neighbors[i]);
    }
}

int main() {
	FILE *in = fopen("sortaret.in", "rt"), *out = fopen("sortaret.out", "wt");
	int n, m, i, x, y;
	ListGraph *lg = malloc(sizeof(ListGraph));
	fscanf(in, "%d %d", &n, &m);
	init_list_graph(lg, n + 1);

	for (i = 0; i < m; ++i) {
        fscanf(in, "%d %d", &x, &y);
        add_edge_list_graph(lg, x, y);
    }
    //print_list_graph(lg);
    topo_sort(lg, out);
    fprintf(out, "\n");

    fclose(in);
    fclose(out);
}