Cod sursa(job #2924578)

Utilizator popescumateicalinPopescu Matei Calin popescumateicalin Data 5 octombrie 2022 15:31:32
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <vector>

std::ifstream in("dfs.in");
std::ofstream out("dfs.out");

const int MAXN = 100010;
int n, m, x, y;
std::vector<std::vector<int>> list(MAXN);
std::vector<bool> vis(MAXN, false);

void dfs(int x)
{
    vis[x] = true;
    for (int v : list[x])
        if (!vis[v])
            dfs(v);
}
int nrconex()
{
    int nr = 0;
    for (int i = 1; i <= n; i++)
        if (!vis[i])
        {
            nr++;
            dfs(i);
        }
    return nr;
}

int main()
{
    in >> n >> m;
    while (in >> x >> y)
    {
        list[x].push_back(y);
        list[y].push_back(x);
    }
    out << nrconex();
    return 0;
}