Cod sursa(job #2245356)

Utilizator andreipredaAndrei Preda andreipreda Data 25 septembrie 2018 09:55:05
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <vector>

#define dim 100005

using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

int n, m, i, j, beginPoint, answer;

bool visited[dim];

vector <int> graph[dim];

void DFS(int node) {

    visited[node] = true;
    for(unsigned int i = 0; i < graph[node].size(); i++) {
        int next = graph[node][i];
        if(!visited[next]) {
            DFS(next);
        }
    }

}

void Read() {

    fin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int x, y;
        fin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for(int i = 1; i <= n; i++) {
        if(!visited[i]) {
            answer ++;
            DFS(i);
        }
    }
    fout << answer;
}

int main() {

    Read();
    return 0;
}