Cod sursa(job #3252048)

Utilizator alecu2008Alecu Alexandru alecu2008 Data 28 octombrie 2024 12:00:21
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <bitset>

using namespace std;

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

const int nmax=1e5;

std::bitset<nmax> viz;

std::vector<std::vector<int>> graf;


int dfs(int nod){
    viz[nod]=1;
    for(int i: graf[nod])
        if(!viz[i])
           dfs(i);

}


int main()
{
    int n,m,i,res=0;
    fin>>n>>m;
    graf.assign(n,std::vector<int>());
    for(i=0;i<m;i++){
        int x,y;
        fin>>x>>y;
        x-=1; y-=1;
        graf[y].emplace_back(x);
        graf[x].emplace_back(y);

    }

    for(i=0;i<n;i++){
        if(!viz[i]){
            dfs(i); res++;
        }
    }

    fout<<res;

    graf.clear();
    return 0;


}