Cod sursa(job #2869849)

Utilizator meinkampfEmanuel Pinzariu meinkampf Data 11 martie 2022 21:17:30
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m, viz[100002];
vector<int> L[100002];

void Citire()
{
    int i, x, y;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
}

void DFS(int k)
{
    viz[k] = 1;
    for (auto e : L[k])
        if (viz[e] == 0) DFS(e);
}

void Driver()
{
    int i, cnt = 0;
    for (i = 1; i <= n; i++)
        if (viz[i] == 0) DFS(i), cnt++;
    fout << cnt << '\n';
}

int main()
{
    Citire();
    Driver();
    fin.close();
    fout.close();
    return 0;
}