Cod sursa(job #613657)

Utilizator dobreDobre Catalin Andrei dobre Data 3 octombrie 2011 01:02:58
Problema Sortare topologica Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 3.88 kb

#include <stdlib.h>
#include <stdio.h>


typedef struct Node_
{
    int     value;
    struct Node_*   next;
}Node;

typedef struct LinkedList_
{
    Node* head;
} LinkedList;

typedef struct Edges_
{
    int deleted;
    int node_id;
    int count_inlinks;
    int count_outlinks;
    LinkedList in_links;
    LinkedList out_links;
}Edges;

FILE *fin,*fout;
int N,M;

Edges * graph;

Node* CreateNode(int value)
{
    Node* node = (Node*)malloc(sizeof(struct Node_));
    node->value = value;
    node->next = NULL;
    return node;
}
LinkedList CreateLinkedList()
{
    LinkedList list;
    list.head = NULL;
    return list;
}
void insert(int value, LinkedList* list)
{
    Node* node = CreateNode(value);
    node->next = list->head;
    list->head = node;
}
void printList(LinkedList list)
{
    Node* current_node = list.head;
    while (current_node != NULL)
    {
        printf("%d ",current_node->value);
        current_node = current_node->next;
    }
}
Edges CreateEdges(int node_id)
{
    Edges edges;
    edges.deleted = 0;
    edges.node_id = node_id;
    edges.in_links = CreateLinkedList();
    edges.out_links = CreateLinkedList();
    edges.count_inlinks = 0;
    edges.count_outlinks = 0;
    return edges;
}
void addEdge(int a, int b)
{
    insert(a,&(graph[b].in_links));
    graph[b].count_inlinks++;
    insert(b,&(graph[a].out_links));
    graph[a].count_outlinks++;
}
void printGraph()
{
    for (int i = 1 ; i < N ; i++)
    {
        printf("[Node %d]\n",i);
        printf("\tInLinks  [%d]\t:",graph[i].count_inlinks);
        printList(graph[i].in_links);
        puts("");
        printf("\tOutLinks [%d]\t:", graph[i].count_outlinks);
        printList(graph[i].out_links);
        puts("");
    }
}
void remove_from_list(LinkedList* list, int node_id)
{
    Node* current = list->head;
    Node* prev = NULL;
    if (current->value == node_id)
    {
        list->head = current->next;
        free(current);
    }
    else
    {
        while (current != NULL)
        {
            prev = current;
            current = current->next;
            if (current->value == node_id)
            {
                prev->next = current->next;
                free(current);
                return;
            }
        }
    }
}
void remove_node(int node_id)
{
    LinkedList* list = &(graph[node_id].in_links);
    Node* current = list->head;
    int count = graph[node_id].count_inlinks;
    for (int i = 0 ; i < count; i++)
    {
        int in_node = current->value;
        remove_from_list(&(graph[in_node].out_links), node_id);
        graph[in_node].count_outlinks--;
        current = current->next;
    }
    list = &(graph[node_id].out_links);
    count = graph[node_id].count_outlinks;
    current = list->head;
    for (int i = 0 ; i < count; i++)
    {
        int out_node = current->value;
        remove_from_list(&(graph[out_node].in_links), node_id);
        graph[out_node].count_inlinks--;
        current = current->next;
    }
    graph[node_id].deleted = 1;
}
void topological_sort(char* filename)
{
    fout = fopen(filename,"w");
    //fputs(fout,"Topological Sort");
    for (int t = 1; t < N; t++)
    {
        for (int i= 1 ; i < N ; i++)
        {
            if (!graph[i].deleted && graph[i].count_inlinks == 0)
            {
                fprintf(fout,"%d ",i);
                remove_node(i);
                break;
            }
        }
    }
    //fputs(fout,"");
    fclose(fout);
}
void read_input(char* filename)
{
    int a,b;
    fin = fopen(filename,"r");
    fscanf(fin,"%d%d",&N,&M);
    N++;
    graph = (Edges*)malloc(sizeof(struct Edges_) * (N + 1));
    for (int i = 1 ; i < N ; i++)
        graph[i] = CreateEdges(i);
    for (int i = 0 ; i < M; i++)
    {
        fscanf(fin,"%d%d",&a,&b);
        addEdge(a,b);
    }
    fclose(fin);
}
int main(int argc, char** argv) {
    read_input("sortaret.in");
    topological_sort("sortaret.out");
    //printGraph();
    return (EXIT_SUCCESS);
}