Cod sursa(job #3161861)

Utilizator TomaBToma Brihacescu TomaB Data 28 octombrie 2023 09:43:00
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

vector<int> edges[100005];
bool visited[100005];

void dfs (int nod)
{
    visited[nod] = 1;
    for (auto u: edges[nod])
        if (!visited[u])
            dfs(u);
}

int main(int argc, char const *argv[])
{
    freopen("dfs.in", "r", stdin);
    freopen("dfs.out", "w", stdout);
    int conex = 0;
    int n, m;
    cin >> n >> m;
    while (m--)
    {
        int x, y;
        cin >> x >> y;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }

    for (int nod = 1; nod <= n; nod++)
    {
        if (!visited[nod])
        {
            conex++;
            dfs(nod);
        }
    }

    cout << conex;
    return 0;
}