Cod sursa(job #3161843)

Utilizator samyro14Samy Dragos samyro14 Data 28 octombrie 2023 09:27:19
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
using namespace  std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");
const int maxn = 1e5 + 2;
const int maxm = 2e5 + 2;
int n, m;
vector<int> a[maxn];
bitset<maxn> visited;
int componente;
void read(){
    fin >> n >> m;
    for(int i = 1; i <= m; ++i){
        int x, y;
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
}
void dfs(int i){
    visited[i] = 1;
   for(auto x : a[i]){
       if(!visited[x])
            dfs(x);
   }
}
int main() {
    read();
    for(int i = 1; i <= n; ++i){
        if(!visited[i])
            dfs(i), componente++;
    }
    fout << componente;
    return 0;
}
/*
 *
 * 4 5 7 9 9 10 12
 *  4 5 7 9
 *
 *
 */