Cod sursa(job #2594993)

Utilizator maria_mMaria Mosneag maria_m Data 6 aprilie 2020 21:09:23
Problema Parcurgere DFS - componente conexe Scor 80
Compilator c-64 Status done
Runda Arhiva educationala Marime 3.07 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NODES 100005

typedef struct Node {
    void* data;
    struct Node *next;
} Node;

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 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++;
}

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_edge_list_graph(ListGraph *graph, int src, int *dest) {
    add_nth_node(graph->neighbors[src], (1 <<30), dest);
}

void dfs_connected_comps(ListGraph *lg, int node, int *visited) {
    Node *p;
    visited[node] = 1;
    for (p = lg->neighbors[node]->head; p; p = p->next) {
    	if (visited[*(int *)p->data] == 0) {
    		dfs_connected_comps(lg, *(int *)p->data, visited);
    	}
    }
}

void connected_components(ListGraph *lg, FILE *out) {
    int visited[MAX_NODES], i, j = 0;
    memset(visited, 0, MAX_NODES);
    for (i = 0; i < lg->nodes; i++) {
    	if (visited[i] == 0) {
    		dfs_connected_comps(lg, i, visited);
    		j++;
    	}
    }
    fprintf(out, "%d\n", j);
}

int main() {
    int nodes, edges;
    int x[MAX_NODES], y[MAX_NODES];
    ListGraph *lg = malloc(sizeof(ListGraph));
    FILE *in = fopen("dfs.in", "rt"), *out = fopen("dfs.out", "wt");

    fscanf(in, "%d%d", &nodes, &edges);
    init_list_graph(lg, nodes);

    for (int i = 0; i < edges; ++i) {
        fscanf(in, "%d%d", &x[i], &y[i]);
        x[i]--;
        y[i]--;
        add_edge_list_graph(lg, x[i], &y[i]);
        add_edge_list_graph(lg, y[i], &x[i]);
    }


    connected_components(lg, out);

    
    free(lg);
    fclose(in);
    fclose(out);
    return 0;
}