Cod sursa(job #3233878)

Utilizator CimpoesuFabianCimpoesu Fabian George CimpoesuFabian Data 5 iunie 2024 12:12:25
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <vector>
#include <bitset>
#include <fstream>
#include <stack>
#include <algorithm>

using namespace std;

vector <int> L[100005];
bitset <100005> viz;

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

int main()
{
    int n, m;
    ifstream fin ("dfs.in");
    fin >> n >> m;
    while (m--)
    {
        int x, y;
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    int nrcc = 0;
    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
        {
            nrcc++;
            DFS(i);
        }
    }
    ofstream fout ("dfs.out");
    fout << nrcc << "\n";
    return 0;
}