Cod sursa(job #3316315)

Utilizator PatrikKev75Szucs Patrik - Kevin PatrikKev75 Data 18 octombrie 2025 12:34:43
Problema Parcurgere DFS - componente conexe Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
// https://infoarena.ro/problema/dfs - Szucs Patrik - Kevin
#include <fstream>
#include <vector>
#include <queue>

void dfs(std::vector<std::vector<int>> graf, std::vector<bool> &vis, int a)
{
    vis[a] = true;
    for (auto elem : graf[a])
    {
        if (!vis[elem])
        {
            dfs(graf, vis, elem);
            vis[elem] = true;
        }
    }
    return;
}

int main()
{
    int n, m;

    std::ifstream in("dfs.in");
    in >> n >> m;

    std::vector<std::vector<int>> graf(n);
    while (m--)
    {
        int x, y;
        in >> x >> y;

        graf[x - 1].push_back(y - 1);
        graf[y - 1].push_back(x - 1);
    }
    in.close();

    int db = 0;
    std::vector<bool> vis(n);
    for (; n > 0; n--)
    {
        if (!vis[n - 1])
        {
            dfs(graf, vis, n - 1);
            db++;
        }
    }

    std::ofstream out("dfs.out");
    out << db;
    out.close();

    return 0;
}