Cod sursa(job #2864963)

Utilizator CiuiGinjoveanu Dragos Ciui Data 8 martie 2022 12:50:41
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <queue>
using namespace std;

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

const int MAX_SIZE = 200005;
vector<int> graph[MAX_SIZE];
int goneThrough[MAX_SIZE], relatedComponents = 0;

void markComponent(int currentPeak) {
    goneThrough[currentPeak] = 1;
    for (int i = 0; i < graph[currentPeak].size(); ++i) {
        if (goneThrough[graph[currentPeak][i]] == 0) {
            markComponent(graph[currentPeak][i]);
        }
    }
}

int main() {
    int peaks, arches;
    fin >> peaks >> arches;
    for (int i = 1; i <= arches; ++i) {
        int start, end;
        fin >> start >> end;
        graph[start].push_back(end);
        graph[end].push_back(start);
    }
    for (int peak = 1; peak <= peaks; ++peak) {
        if (goneThrough[peak] == 0) {
            ++relatedComponents;
            markComponent(peak);
        }
    }
    fout << relatedComponents;
    return 0;
}