Cod sursa(job #3352657)

Utilizator TonyyAntonie Danoiu Tonyy Data 30 aprilie 2026 09:36:30
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

const int nMax = 1e5 + 1;
int n, m;
vector<int> list[nMax];
bool visited[nMax];

void dfs(int node) {
    visited[node] = 1;
    for (int i : list[node]) {
        if (!visited[i]) {
            dfs(i);
        }
    }
}

int main() {
    fin >> n >> m;
    for (int i = 0; i < m; i++) {
        int x, y;
        fin >> x >> y;
        list[x].push_back(y);
        list[y].push_back(x);
    }
    int cc = 0;
    for (int node = 1; node <= n; node++) {
        if (!visited[node]) {
            cc++;
            dfs(node);
        }
    }
    fout << cc;

    fin.close();
    fout.close();
    return 0;
}