Cod sursa(job #2199435)

Utilizator Raoul_16Raoul Bocancea Raoul_16 Data 27 aprilie 2018 17:55:05
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <bits/stdc++.h>
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].push_back(y);
        graph[y].push_back(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 x : graph[node])
        if(!use[x]){
            ++nr;
            dfs(x);
        }
}