Cod sursa(job #2823325)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 28 decembrie 2021 01:02:08
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, s, fr[100005], nrN;
vector<int>h[100005];

void Dfs(int x)
{
    fr[x] = 1;
    for(auto i : h[x])
        if(fr[i] == 0)
            Dfs(i);
}

int main()
{
    int x, y;
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        h[x].push_back(y);
        h[y].push_back(x);
    }
    for(int i = 1; i <= n; i++)
        if(fr[i] == 0)
    {
        nrN++;
        Dfs(i);
    }
    fout << nrN;
    return 0;
}