Cod sursa(job #2712302)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 25 februarie 2021 16:47:15
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int NMax = 1e5;

vector <int> g[NMax + 5];

int n, m, ans;
bool use[NMax + 5];

void Read(){
    fin >> n >> m;
    while (m--){
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}
void DFS(int node){
    use[node] = 1;
    for (auto ngh : g[node])
        if (!use[ngh])
            DFS(ngh);
}

void Solve(){
    for (int i = 1; i <= n; i++)
        if (!use[i]){
            ans++;
            DFS(i);
        }
}

void Print(){
    fout << ans << '\n';
}

int main(){
    Read();
    Solve();
    Print();
    return 0;
}