Cod sursa(job #2763436)

Utilizator Teodor_AxinteAxinte Teodor-Ionut Teodor_Axinte Data 14 iulie 2021 08:43:57
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
#include <iostream>
#include <bitset>

using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

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

int n, m, ct;
node *L[100010];
bitset<100010> viz;

void add_begin(node *&L, int integer) {
    node *l = new node;
    l->value = integer;
    l->next = L;
    L = l;
}


void dfs(node *L[], int &current_node) {
    viz[current_node] = 1;
    while (L[current_node] != nullptr)
    {
        if(!viz[L[current_node]->value])
            dfs(L,L[current_node]->value);
        L[current_node]=L[current_node]->next;
    }
}


int main() {

    fin >> n >> m;
    //node *L= new node [n];

    for (; m != 0; m--) {
        int x, y;
        fin >> x >> y;
        add_begin(L[x], y);
        add_begin(L[y], x);
    }

    for (int i = 1; i <= n; i++)
        if (!viz[i]) {
            viz[i] = true;
            dfs(L, i);
            ct++;
        }

    fout << ct;

    return 0;
}