Cod sursa(job #3313555)

Utilizator CimpoesuFabianCimpoesu Fabian George CimpoesuFabian Data 5 octombrie 2025 11:49:28
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");

int n, m, viz[100001];
vector <int> G[200001];

void DFS(int nod)
{
    for (auto next : G[nod])
        if (viz[next] == 0)
    {
        viz[next] = 1;
        DFS(next);
    }
}

int main()
{
    int x, y, i, cnt = 0;
    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;
    return 0;
}