Cod sursa(job #2564691)

Utilizator radugheoRadu Mihai Gheorghe radugheo Data 2 martie 2020 09:20:30
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, i, x, y, cc;
int f[100005];

vector <int> L[100005];

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

int main(){
    fin >> n >> m;
    for (i=1; i<=m; i++){
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    for (i=1; i<=n; i++){
        if (f[i] == 0){
            cc++;
            dfs (i);

        }
    }
    fout << cc;
    return 0;
}