Cod sursa(job #3246664)

Utilizator stefanpetre004Petre Stefan stefanpetre004 Data 3 octombrie 2024 22:03:01
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

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

const int NMAX = 1e5;
std::vector<int> L[NMAX + 1];
int dist[NMAX +  1];
int viz[NMAX + 1];

void dfs(int x){
    viz[x] = 1;
    for (auto next: L[x]){
        if(!viz[next])
            dfs(next);
    }
}
int main() {
    int N, M;
    f >> N >> M;
    for(int i = 1; i <= M; ++i){
        int x, y;
        f >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    int cc = 0;
    for (int i = 1; i <= N; ++i)
        if (!viz[i]){
            dfs(i);
            cc++;
        }
    g << cc;
    return 0;
}