Cod sursa(job #3215581)

Utilizator victor_gabrielVictor Tene victor_gabriel Data 15 martie 2024 10:16:58
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
#include <vector>
#include <climits>
#include <queue>

using namespace std;

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

const int DIM = 100010;

int n, m, s, x, y;
vector<int> l[DIM];
bool visited[DIM];

void dfs(int node) {
    visited[node] = true;
    for (auto adjNode : l[node])
        if (!visited[adjNode])
            dfs(adjNode);
}

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

    int conCompCnt = 0;
    for (int node = 1; node <= n; node++) {
        if (!visited[node]) {
            conCompCnt++;
            dfs(node);
        }
    }

    fout << conCompCnt;

    return 0;
}