Cod sursa(job #2749435)

Utilizator DVDPRODavid D DVDPRO Data 6 mai 2021 17:55:43
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <vector>
#include <bitset>
#include <fstream>

using namespace std;

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

#define NMAX 100005
int n, m, nr;
vector<int> tree[NMAX];
bitset<NMAX> check;

void read()
{
    fin>>n>>m;
    int a, b;
    for(int i=0; i<m; i++)
    {
        fin>>a>>b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }
}

void HowdyNeighbour(int node)
{
    check.set(node);
    for(auto &neighbour:tree[node])
        if(!check[neighbour])
            HowdyNeighbour(neighbour);
}

void walk()
{
    for(int i=1; i<=n; i++)
        if(!check[i])
    {
        nr++;
        HowdyNeighbour(i);
    }
}

int main()
{
    read();
    walk();
    fout<<nr;
    return 0;
}