Cod sursa(job #2366026)

Utilizator qwerty1234qwerty qwerty1234 Data 4 martie 2019 18:08:19
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");

const int Nmax = 100005;

vector < int > L[Nmax];

int n , m;

bool viz[Nmax];

void DFS(int node)
{
    viz[node] = true;
    for(auto it : L[node])
        if(!viz[it])
            DFS(it);
}

int main()
{
    int x , y , ans = 0;
    fin >> n >> m;
    for(int i = 1 ; i <= m ; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    for(int i = 1 ; i <= n ; i++)
        if(!viz[i])
            ++ans , DFS(i);
    fout << ans << "\n";
    fin.close();
    fout.close();
    return 0;
}