Cod sursa(job #2553924)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 22 februarie 2020 13:17:19
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <bits/stdc++.h>
using namespace std;

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

const int N = 1e5 + 5;

vector<int> g[N];
bool f[N];

int n, m;

void dfs(int nod) {
    f[nod] = true;
    for (auto ngh: g[nod]) if (!f[ngh])
        dfs(ngh);
}


int main() {
    int ans = 0;

    fi >> n >> m;
    for (int a, b, i = 1; i <= m; ++i) {
        fi >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

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

    fo << ans << '\n';
    
    return 0;
}