Cod sursa(job #2981016)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 17 februarie 2023 01:57:48
Problema Parcurgere DFS - componente conexe Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>
#include <vector>
#include <bitset>

#define DIM 100006

using namespace std;

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

int n, m;
vector<int> edges[DIM];
bitset<DIM> fr;

void dfs(int node) {
    fr[node] = true;
    for (auto newNode: edges[node])
        if (!fr[newNode]) {
            dfs(newNode);
    }
}

int main() {
    f >> n >> m;

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

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

    g << cnt;

    return 0;
}