Cod sursa(job #3163976)

Utilizator TonyyAntonie Danoiu Tonyy Data 1 noiembrie 2023 20:07:44
Problema Parcurgere DFS - componente conexe Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
#include <bitset>
#include <vector>
#define Max 100001
using namespace std;

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

bitset <Max> viz;
vector <int> graph[Max];

int n, m, c;

void read()
{
    fin >> n >> m;
    for (int i = 0 ; i < m ; ++i)
    {
        int x, y;
        fin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
}

void dfs (int node)
{
    viz[node] = 1;
    for (auto i : graph[node])
        if (!viz[i])
            dfs(i);
}

int main()
{
    read();
    for (int i = 0 ; i < n ; ++i)
        if (!viz[i])
        {
            ++c;
            dfs(i);
        }
    fout << c;

    fin.close(); 
    fout.close();
    return 0;
}