Cod sursa(job #2700699)

Utilizator codrut86Coculescu Ioan-Codrut codrut86 Data 28 ianuarie 2021 15:12:03
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
#include <stdbool.h>

using namespace std;

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

const int N = 100001;

vector<int> a[N];
bool viz[N];
int n, m, nrc;

void dfs(int x)
{
    viz[x] = true;

    for(auto y : a[x])
    {
        if(!viz[y])
        {
            dfs(y);
        }
    }
}

int main()
{
    in >> n >> m;

    for(int i = 0; i < m; i++)
    {
        int x, y;
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
        {
            nrc++;
            dfs(i);
        }
    }

    out << nrc;

    return 0;
}