Cod sursa(job #2884479)

Utilizator hobbitczxdumnezEU hobbitczx Data 3 aprilie 2022 19:26:01
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>
#define ll long long
#define INF 0x3F3F3F3F
using namespace std;

const string fisier = "dfs";

ifstream fin (fisier + ".in");
ofstream fout (fisier + ".out");

const int N_MAX = 1e5 + 5;

vector<int>g[N_MAX];
int n , m , ans;
bool used[N_MAX];

void dfs (int node){
    used[node] = true;
    for (auto i : g[node]){
        if (used[i] == false){
            dfs(i);
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    fin >> n >> m;
    for (int i=1; i<=m; i++){
        int x , y; fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i=1; i<=n; i++){
        if (used[i] == false){
            dfs(i);
            ans += 1;
        }
    }
    fout << ans;
}