Cod sursa(job #3172201)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 20 noiembrie 2023 12:09:01
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 f ("dfs.in");
ofstream g ("dfs.out");

const int NMAX = 1e5;
bool cmap[NMAX+1];
vector<int> v[NMAX+1];

void dfs(int i){
    cmap[i] = true;
    for(int con : v[i])
        if(!cmap[con])
            dfs(con);
}

int main()
{
    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 cnt = 0;
    
    for(int i=1; i<=n; i++)
        if(!cmap[i])
            cnt ++, dfs(i);
    
    g << cnt;
    
    return 0;
}