Cod sursa(job #2288994)

Utilizator q1e123Solca Robert-Nicolae q1e123 Data 24 noiembrie 2018 10:11:43
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <cstdio>
#include <bitset>
#include <vector>

using std::bitset;
using std::vector;

const int MAX = 100002;

int n,m,nr;
vector<int> G [MAX];
bitset<MAX> viz;

void DFS(int node){
    viz[node]=true;
    for(auto i:G[node]){
        if(!viz[i]){
            DFS(i);
        }
    }
}

int main() {
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);

    scanf("%d %d",&n,&m);

    for(int i=0;i<m;++i){
        int x,y;
        scanf("%d %d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for(int i=1;i<=n;++i){
        if(!viz[i]){
            ++nr;
            DFS(i);
        }
    }
    printf("%d",nr);
    return 0;
}