Cod sursa(job #2929661)

Utilizator DKMKDMatei Filibiu DKMKD Data 26 octombrie 2022 14:00:06
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

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

void citire()
{
    in >> n >> m;
    int x, y;
    for (int i = 1; i <= m; i++)
    {
        in >> x >> y;
        l[x].push_back(y);
        l[y].push_back(x);
    }

}

void dfs(int nod)
{
    viz[nod] = 1;
    for (int i = 0; i < l[nod].size(); i++)
    {
        if (viz[l[nod][i]] == 0)
        {
            dfs(l[nod][i]);
        }
    }
}

int main()
{
    int cc = 0;
    citire();

    for (int i = 1; i <= n; i++)
    {
        if (viz[i] == 0)
        {
            cc++;
            dfs(i);
        }
    }
    out << cc;

    return 0;
}