Cod sursa(job #2952533)

Utilizator Razvan_GabrielRazvan Gabriel Razvan_Gabriel Data 9 decembrie 2022 15:12:36
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <vector>
#include <bitset>
#include <fstream>

using namespace std;

const int N = 1e5;
const int M = 2e5;

vector <int> a[N + 1];
bitset <N + 1> viz;

void dfs(int x){
    viz[x] = 1;
    for(auto y:a[x])
        if(!viz[y])
            dfs(y);
}

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

    int n, m;
    fin >> n >> m;

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

    int nrc = 0;
    for(int i = 1; i <= n; i++){
        if(!viz[i]){
            nrc++;
            dfs(i);
        }
    }

    fout << nrc;

    return 0;
}