Cod sursa(job #3284732)

Utilizator crina2120Arnautu Cristina-Crina crina2120 Data 12 martie 2025 09:42:48
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m, cnt, viz[100003];
vector <int> g[100003];

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

int main()
{
    int i, x, y;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
        {
            cnt++;
            DFS(i);
        }
    fout << cnt << "\n";
    return 0;
}
//0 1 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 21 22 24 25 26 27 29 30 34 35 36 42 44 45 47 48 51 54 56 57 58