Cod sursa(job #3144856)

Utilizator rxdgxnGantoi Radu rxdgxn Data 10 august 2023 21:39:01
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <vector>
#include <unordered_map>
using namespace std;

// https://infoarena.ro/problema/dfs

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

unordered_map<int, vector<int>> graph;
int mp[100001];

void dfs(int k) {
    mp[k] = 1;
    for (int x : graph[k]) {
        if (!mp[x]) dfs(x);
    }
}

int main(void) {

    int n, m, x, y, t = 0;
    f >> n >> m;

    for (int i = 0; i < m; i++) {
        f >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }

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

    g << t;

    return 0;
}