Cod sursa(job #2789522)

Utilizator Teodor_AxinteAxinte Teodor-Ionut Teodor_Axinte Data 27 octombrie 2021 17:00:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

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

vector<int> v[100010];
bitset<100010> viz;
int ct;

void add_edge(int x, int y) {
    v[x].push_back(y);
    v[y].push_back(x);
}

void dfs(int node) {
    viz[node] = 1;
    for (auto it: v[node])
        if (!viz[it])
            dfs(it);
}

int main() {
    int n, m;
    fin >> n >> m;
    for (; m != 0; m--) {
        int x, y;
        fin >> x >> y;
        add_edge(x, y);
    }

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

    fout << ct;

    return 0;

}