Cod sursa(job #2139668)

Utilizator inquisitorAnders inquisitor Data 22 februarie 2018 18:12:08
Problema Parcurgere DFS - componente conexe Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <cstdio>
#include <vector>
#include <malloc.h>

struct node
{
    int value; node *next;
};

int vertices, edges, u, v, conexParts;

node *adj[100001]; bool visited[4096];

inline void DFS(int current)
{
    visited[current] = true;

    for(node *child = adj[current]; child; child = child -> next)
    {
        if(!visited[child -> value]) DFS(child -> value);
    }
}

#define SIZE 0x55730

__attribute__((always_inline)) void read(int &num)
{
    static char inBuffer[SIZE];

    static unsigned int p = ~-SIZE; num = 0x0;

    while(inBuffer[p] < 0x30 | inBuffer[p] > 0x39)
    {
        ++p != SIZE || (fread(inBuffer, 0x1, SIZE, stdin), p = 0x0);
    }

    while(inBuffer[p] > 0x2F & inBuffer[p] < 0x3A)
    {
        num = num * 0xA + inBuffer[p] - 0x30;

        ++p != SIZE || (fread(inBuffer, 0x1, SIZE, stdin), p = 0x0);
    }
}

int main()
{
    freopen("dfs.in", "r", stdin);
    freopen("dfs.out", "w", stdout);

    read(vertices); read(edges);

    for(int i = edges; i; --i)
    {
        read(u), read(v);

        node *newbie1 = (node*)malloc(8);

        newbie1 -> value = u;

        newbie1 -> next  = adj[v];

        adj[v] = newbie1;

        node *newbie2 = (node*)malloc(8);

        newbie2 -> value = v;

        newbie2 -> next  = adj[u];

        adj[u] = newbie2;
    }

    for(int i = vertices; i; --i)
    {
        if(!visited[i])
        {
            DFS(i);

            ++conexParts;
        }
    }

    printf("%d", conexParts);

    return 0;
}