Cod sursa(job #3264933)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 25 decembrie 2024 20:16:00
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

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

const int nmax = 100001;

int n,m;
array<vector<int>,nmax> g;
int ans[nmax];
bool vis[nmax];

void dfs(const int x) {
    vis[x] = true;
    for (const auto y : g[x]) {
        if (!vis[y]) {
            dfs(y);
        }
    }
}

int main() {
    fin >> n >> m;
    while (m--) {
        int a,b;
        fin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            ans++;
            dfs(i);
        }
    }

    fout << ans;

    return 0;
}