Cod sursa(job #2793257)

Utilizator francescom_481francesco martinut francescom_481 Data 3 noiembrie 2021 13:09:43
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");
#define cin fin
#define cout fout

#define N 100005
vector < vector < int > > g;
int n, m, nr, x, y, f[N];

void fa(int nod)
{
    f[nod] = 1;
    for(int i = 0 ; i < g[nod].size() ; i++)
    {
        if(f[g[nod][i]] == 0)
        {
            fa(g[nod][i]);
        }
    }
}

int main()
{
    cin >> n >> m;
    g.resize(n+5);
    for(int i = 1 ; i <= m ; i++)
    {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for(int i = 1 ; i <= n ; i++)
    {
        if(f[i] == 0)
        {
            nr++;
            fa(i);
        }
    }
    cout << nr;
    return 0;
}