Cod sursa(job #2587104)

Utilizator k2e0e0w3qDumitrescu Gheorghe k2e0e0w3q Data 22 martie 2020 00:22:40
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
#define N 100001
using namespace std;

bitset <N> seen;
vector <int> G[N];

void DFS (int i) {
    seen[i]=1;
    for (auto it: G[i])
        if (!seen[it])
            DFS(it);
}

int main () {
    ifstream fin ("dfs.in");
    ofstream fout ("dfs.out");
    ios::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    int n, m, i, j;
    fin >> n >> m;
    for (; m; m--) {
        fin >> i >> j;
        G[i].push_back(j);
        G[j].push_back(i); //hi hi
    }

    int ct=0;
    for (i=1; i<=n; i++)
        if (!seen[i])
            ++ct,
            DFS(i);
    fout << ct;
    return 0;
}