Cod sursa(job #2639987)

Utilizator Iustin01Isciuc Iustin - Constantin Iustin01 Data 4 august 2020 17:12:28
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <bits/stdc++.h>
#define MAX 1000005
using namespace std;

ifstream in("dfs.in");
ofstream out("dfs.out");

vector < int > g[MAX];
int n, m, x, y, nrc;
bitset < MAX > c;

void dfs(int k){
    c[k] = true;
    for(int i = 0; i < g[k].size(); i++){
        int vec = g[k][i];
        if(!c[vec])
            dfs(vec);
    }
}

int main(){
    in>>n>>m;
    for(int i = 1; i <= m; i++){
        in>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for(int i = 1; i <= n; i++)
        if(!c[i])
            nrc++, dfs(i);
    out<<nrc;
}