Cod sursa(job #3317144)

Utilizator cezar12345Ciocirlan Cezar-Gabriel cezar12345 Data 22 octombrie 2025 16:24:25
Problema Parcurgere DFS - componente conexe Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <iostream>
#include <fstream>
#include <vector>
std::ifstream in("dfs.in");
std::ofstream out("dfs.out");

const int NMAX = 100000;
int vis[NMAX + 1];
std::vector<int> la[NMAX + 1];

void DFS(int nod) {
    vis[nod] = 1;
    for(int x : la[nod]) {
        if(vis[x] == 0) DFS(x);
    }
}

int main() {
    int n, m, x, y, nr = 0; 
    in >> n >> m;
    for(int i = 0; i < m; i++) {
        in >> x >> y;
        la[x].push_back(y);
        la[y].push_back(x);
    }
    for(int i = 1; i <= n; i++)
    {
        if(vis[i] == 0) {
            nr++;
            DFS(x);
        }
    }
    out << nr;
    return 0;
}