Cod sursa(job #2680483)

Utilizator GligarEsterabadeyan Hadi Gligar Data 3 decembrie 2020 17:09:16
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int nmax=100000;

vector <int> g[nmax+1];

bool u[nmax+1];

void dfs(int x){
    u[x]=1;
    for(int i=0;i<int(g[x].size());i++){
        int xn=g[x][i];
        if(u[xn]==0){
            dfs(xn);
        }
    }
}

int main(){
    int n,m;
    fin>>n>>m;
    for(int i=1;i<=m;i++){
        int x,y;
        fin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    int sol=0;
    for(int i=1;i<=n;i++){
        if(u[i]==0){
            dfs(i);
            sol++;
        }
    }
    fout<<sol<<"\n";
    return 0;
}