Cod sursa(job #2924723)

Utilizator papinub2Papa Valentin papinub2 Data 9 octombrie 2022 14:30:50
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int Nmax = 100005;

vector<int> A[Nmax + 5];

void DFS (int nod, vector<bool>&viz)
{
    viz[nod] = true;
    for (int i = 0; i < A[nod].size(); i++)
    {
        int nod_curent = A[nod][i];
        if (viz[nod_curent] == false)
            DFS(nod_curent, viz);
    }
}

int main()
{
    int n, m, rez = 0;
    in >> n >> m;

    vector<bool> viz(n + 5);

    for (int i = 1; i <= m; i++)
    {
        int x, y;
        in >> x >> y;

        A[x].push_back(y);
        A[y].push_back(x);
    }

    for (int i = 1; i <= n; i++)
        if (viz[i] == false)
        {
            DFS(i, viz);
            rez++;
        }

    out << rez;
    return 0;
}