#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NODES 100
typedef struct Node {
void *data;
struct Node *next;
} Node;
typedef struct {
Node *head;
Node *tail;
int size;
} LinkedList;
typedef struct {
LinkedList **neighbors;
int nodes;
} ListGraph;
typedef struct {
LinkedList *list;
} Queue;
void init_list(LinkedList *list);
void add_nth_node(LinkedList *list, int n, void *new_data);
Node* remove_nth_node(LinkedList *list, int n);
int get_size(LinkedList *list);
void free_list(LinkedList **list);
void print_int_linkedlist(LinkedList *list);
void print_string_linkedlist(LinkedList *list);
void init_list_graph(ListGraph *graph, int nodes);
void add_edge_list_graph(ListGraph *graph, int src, int *dest);
int has_edge_list_graph(ListGraph *graph, int src, int dest);
LinkedList* get_neighbours_list_graph(ListGraph *graph, int node);
void remove_edge_list_graph(ListGraph *graph, int src, int dest);
void clear_list_graph(ListGraph *graph);
void init_q(Queue *q);
int get_size_q(Queue *q);
int is_empty_q(Queue *q);
void* front(Queue *q);
void dequeue(Queue *q);
void enqueue(Queue *q, void *new_data);
void clear_q(Queue *q);
void purge_q(Queue *q);
void init_list(LinkedList *list) {
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
void add_nth_node(LinkedList *list, int n, void *new_data) {
Node *prev, *curr;
Node *new_node;
if (list == NULL) {
return;
}
/* n >= list->size inseamna adaugarea unui nou nod la finalul listei. */
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(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) {
/* Adica n == 0. */
list->head = new_node;
} else {
prev->next = new_node;
}
if (new_node->next == NULL) {
list->tail = new_node;
}
list->size++;
}
Node* remove_nth_node(LinkedList *list, int n) {
Node *prev, *curr;
if (list == NULL) {
return NULL;
}
if (list->head == NULL) { /* Lista este goala. */
return NULL;
}
/* n >= list->size - 1 inseamna eliminarea nodului de la finalul listei. */
if (n > list->size - 1) {
n = list->size - 1;
} else if (n < 0) {
return NULL;
}
curr = list->head;
prev = NULL;
while (n > 0) {
prev = curr;
curr = curr->next;
--n;
}
if (prev == NULL) {
/* Adica n == 0. */
list->head = curr->next;
} else {
prev->next = curr->next;
if (prev->next == NULL) {
list->tail = prev;
}
}
list->size--;
return curr;
}
int get_size(LinkedList *list) {
if (list == NULL) {
return -1;
}
return list->size;
}
void free_list(LinkedList **pp_list) {
struct Node *currNode;
if (pp_list == NULL || *pp_list == NULL) {
return;
}
while(get_size(*pp_list) > 0) {
currNode = remove_nth_node(*pp_list, 0);
free(currNode);
}
free(*pp_list);
*pp_list = NULL;
}
void print_int_linkedlist(LinkedList *list) {
Node *curr;
if (list == NULL) {
return;
}
curr = list->head;
while (curr != NULL) {
printf("%d ", *((int*)curr->data));
curr = curr->next;
}
printf("\n");
}
void print_string_linkedlist(LinkedList *list) {
Node *curr;
if (list == NULL) {
return;
}
curr = list->head;
while (curr != NULL) {
printf("%s ", (char*)curr->data);
curr = curr->next;
}
printf("\n");
}
void init_list_graph(ListGraph *graph, int nodes) {
graph->nodes = nodes;
graph->neighbors = calloc(nodes, sizeof(LinkedList));
if (graph->neighbors == NULL) {
perror("Not enough memory to initialize the adjacency list!");
exit(-1);
}
for (int i = 1; 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_edge_list_graph(ListGraph *graph, int src, int *dest) {
add_nth_node(graph->neighbors[src], (1 <<30), dest);
}
int has_edge_list_graph(ListGraph *graph, int src, int dest) {
Node *head = graph->neighbors[src]->head;
int crt_node;
while (head != NULL) {
crt_node = *(int *)head->data;
if (crt_node == dest) {
return 1;
}
head = head->next;
}
return 0;
}
LinkedList* get_neighbours_list_graph(ListGraph *graph, int node) {
return graph->neighbors[node];
}
void remove_edge_list_graph(ListGraph *graph, int src, int dest) {
Node *head = graph->neighbors[src]->head;
int node_index = 0;
int crt_node = 0;
while (head != NULL) {
crt_node = *(int *)head->data;
if (crt_node == dest) {
remove_nth_node(graph->neighbors[src], node_index);
return;
}
head = head->next;
++node_index;
}
}
void clear_list_graph(ListGraph *graph) {
for (int i = 0; i < graph->nodes; ++i) {
free_list(&graph->neighbors[i]);
}
free(graph->neighbors);
}
void init_q(Queue *q) {
q->list = malloc(sizeof(LinkedList));
if (q == NULL) {
perror("Not enough memory to initialize the queue!");
exit(-1);
}
init_list(q->list);
}
int get_size_q(Queue *q) {
return q->list->size;
}
int is_empty_q(Queue *q) {
return get_size_q(q) == 0;
}
void* front(Queue *q) {
if (q == NULL || q->list == NULL) {
return NULL;
}
return q->list->head->data;
}
void dequeue(Queue *q) {
struct Node *node;
if (q == NULL || q->list == NULL) {
return;
}
node = remove_nth_node(q->list, 0);
free(node);
}
void enqueue(Queue *q, void *new_data) {
add_nth_node(q->list, q->list->size, new_data);
}
void clear_q(Queue *q) {
struct Node *node;
while (!is_empty_q(q)) {
node = remove_nth_node(q->list, 0);
free(node);
}
}
void purge_q(Queue *q) {
clear_q(q);
free(q->list);
}
void dfs_list_graph(ListGraph *lg, int node, int *visited, Queue *q) {
Node *pnt;
int *N = malloc(sizeof(int));
*N = node;
visited[node] = 1;
//if (lg->neighbors[node]->head == NULL) return;
pnt = lg->neighbors[node]->head;
enqueue(q, N);
while (pnt != NULL) {
if (visited[*(int*)pnt->data] == 0) {
dfs_list_graph(lg, *(int*)pnt->data, visited, q);
}
pnt = pnt->next;
}
}
int main() {
int nodes, edges, *visited;
int nodes_index[MAX_NODES];
int x[MAX_NODES], y[MAX_NODES];
ListGraph *lg = malloc(sizeof(ListGraph));
Queue *q = malloc(sizeof(Queue));
FILE *in, *out;
in = fopen("sortaret.in", "rt");
fscanf(in, "%d %d", &nodes, &edges);
init_list_graph(lg, nodes);
for (int i = 1; i <= nodes; ++i) {
nodes_index[i] = i;
}
for (int i = 1; i <= edges; ++i) {
fscanf(in, "%d %d", &x[i], &y[i]);
add_edge_list_graph(lg, x[i], &y[i]);
}
fclose(in);
out = fopen("sortaret.out", "wt");
init_q(q);
visited = calloc(nodes, sizeof(int));
for (int i = 1; i <= nodes; i++) {
if (visited[i] == 0) {
dfs_list_graph(lg, i, visited, q);
}
}
while (!is_empty_q(q)) {
fprintf(out, "%d ", *(int*)front(q));
dequeue(q);
}
fprintf(out, "\n");
fclose(out);
return 0;
}