Cod sursa(job #3212694)

Utilizator PescarusTanislav Luca Andrei Pescarus Data 12 martie 2024 08:53:11
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>
#include <vector>
using namespace std;
const int nmax = 100005;
vector<int> v[nmax];
bool viz[nmax];

void dfs(int node){
    viz[node] = true;
    for(auto vec: v[node]){
        if(viz[vec] == 0){
            dfs(vec);
        }
    }
}
int main(){
    ifstream f("dfs.in");
    ofstream g("dfs.out");
    int n, m;
    f >> n >> m;
    for(int i = 1; i <= m; i++){
        int x, y;
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    int sol = 0;
    for(int i = 1; i <= n; i++){
        if(viz[i] == 0){
            sol++;
            dfs(i);
        }
    }
    g << sol << '\n';

}