Cod sursa(job #2916595)

Utilizator Katherine456719Swan Katherine Katherine456719 Data 30 iulie 2022 18:36:26
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <fstream>
using namespace std;

int n,m;
vector<int>graph[100005];
bool viz[100005];

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

int main() {
    ifstream fin("dfs.in");
    ofstream fout("dfs.out");
    fin >> n >> m;
    while(m--){
        int x,y;
        fin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }

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