Cod sursa(job #3341146)

Utilizator ioanabaduIoana Badu ioanabadu Data 18 februarie 2026 10:51:57
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <climits>
#define NMAX 100005

using namespace std;

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

vector <int> graph[NMAX];
int nodes, edges;
bool viz[NMAX];

void dfs (int node){
    viz[node] = true;

    for (auto idx:graph[node])
        if (viz[idx] == false)
            dfs(idx);
}

int main (){
    int x, y;

    in >> nodes >> edges;
    for (int i=1; i<=edges; ++i){
        in >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }

    int rez = 0;
    for (int i=1; i<=nodes; ++i){
        if (viz[i] == false){
            dfs(i);
            ++rez;
        }
    }

    out << rez;

    return 0;
}