Cod sursa(job #2197335)

Utilizator Raoul_16Raoul Bocancea Raoul_16 Data 21 aprilie 2018 19:26:04
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <bits/stdc++.h>
#define PB push_back
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector<int> graph[100005];
bool use[100005];
int N,M;
int x,y;
int nr=0;
void dfs(int);

int main(){
    f>>N>>M;
    for(int i=1;i<=M;++i){
        f>>x>>y;
        graph[x].PB(y);
        graph[y].PB(x);
    }
    for(int i=1;i<=N;++i)
        if(!use[i])
            dfs(i);
    g<<nr;
    return 0;
}

void dfs(int node){
    use[node]=true;
    for(int i:graph[node])
        if(!use[i]){
            ++nr;
            dfs(i);
    }
}