Cod sursa(job #3142984)

Utilizator PostoacaMateiMatei Postoaca PostoacaMatei Data 26 iulie 2023 16:30:30
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int n, m, nr;
vector<int> gf[100001];
int viz[100001];

void DFS(int x) {
    viz[x] = 1;
    for (int i : gf[x])
        if (!viz[i])
            DFS(i);
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        fin >> x >> y;
        gf[x].push_back(y);
        gf[y].push_back(x);
    }
    for (int i = 1; i <= n; i++)
        if (!viz[i])
            nr++, DFS(i);
    fout << nr;
    return 0;
}