Cod sursa(job #1919152)

Utilizator RaresEGaySopterean Adrian RaresEGay Data 9 martie 2017 18:08:40
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <iostream>
#include <vector>

#define maxn 100005
#define INF 0x3f3f3f3f

using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

vector <int> v[maxn];

int n, m, sol;
int visited[maxn];

void DFS(int current){
    visited[current] = 1;
    for(int i = 0; i < v[current].size(); ++i){
        int nod = v[current][i];
        cout << nod << ' ';
        if(!visited[nod]){
            DFS(nod);
        }
    }
}

int main(){
    f >> n >> m;
    for(int i = 1; i <= m; ++i){
        int x, y;
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i = 1; i <= n; ++i){
        if(!visited[i]){
            DFS(i);
            ++sol;
        }
    }
    g << sol << '\n';

}