Cod sursa(job #2201909)

Utilizator Raoul_16Raoul Bocancea Raoul_16 Data 6 mai 2018 15:15:56
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>
#define MAX 100005
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector<int> graph[MAX];
bool use[MAX];
int N,M;
int x,y;
int nr=0;
void read();
void dfs(int);
int main(){
    read();
    for(int i=1;i<=N;++i)
        if(!use[i]){
            ++nr;
            dfs(i);
        }
    g<<nr;
    return 0;
}

void read(){
    f>>N>>M;
    for(int i=1;i<=M;++i){
        f>>x>>y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
}

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