Cod sursa(job #2657988)

Utilizator modulopaulModulopaul modulopaul Data 12 octombrie 2020 20:37:02
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>
#include <vector>
#define MAXM 200001
#define MAXN 100001
#define uint unsigned int

using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector <int> MC[MAXM];
bool went[MAXM];
int n,m,nrelconex;
void DFS(int nod){
    went[nod]=1;
    for(uint i=0;i<MC[nod].size();i++){
        int next=MC[nod][i];
        if(went[next]==false){
            DFS(next);
        }
    }
}
int main(){
    fin>>n>>m;
    for(int i=1;i<=m;i++){
        int x,y;
        fin>>x>>y;
        MC[x].push_back(y);
        MC[y].push_back(x);
    }
    for(int i=1;i<=n;i++){
        if(went[i]==0){
            DFS(i);
            nrelconex++;
        }
    }
    fout<<nrelconex;
    return 0;
}