Cod sursa(job #3318474)

Utilizator temp1234Temp Mail temp1234 Data 28 octombrie 2025 12:50:08
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<int> l[100001];
bool viz[100001];

void dfs(int nod) {
    viz[nod] = true;
    for (auto v : l[nod]) {
        if (!viz[v]) {
            dfs(v);
        }
    }
}

int main() {

    int n, m;
    fin >> n >> m;
    for (int i = 1; i <= m; i ++) {
        int x, y;
        fin >> x >> y;
        l[x].push_back(y);
        l[y].push_back(x);
    }

    int sol = 0;
    for (int i = 1; i <= n; i ++) {
        if(!viz[i]) {
            sol ++;
            dfs(i);
        }
    }

    fout << sol << "\n";

    return 0;
}