Cod sursa(job #2245354)

Utilizator andreipredaAndrei Preda andreipreda Data 25 septembrie 2018 09:52:24
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 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(int i = 1; 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 <= m; i++) {
        if(!visited[i]) {
            answer ++;
            DFS(i);
        }
    }
    fout << answer;
}

int main() {

    Read();

    return 0;
}