Cod sursa(job #2596190)

Utilizator CharacterMeCharacter Me CharacterMe Data 9 aprilie 2020 13:28:46
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>

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

int n, m, sol;
vector<int> graph[100005];
bitset<100005> check;

void dfs(int now);

int main()
{

    fin >> n >> m;

    while(m--){
        int x, y;
        fin >> x >> y;

        graph[x].push_back(y);
        graph[y].push_back(x);
    }

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

    fout << sol;

    return 0;
}

void dfs(int now){
    check[now] = true;

    for(auto next:graph[now]){
        if(check[next]) continue;

        dfs(next);
    }

}