Cod sursa(job #2321220)

Utilizator andra_moldovanAndra Moldovan andra_moldovan Data 15 ianuarie 2019 20:35:58
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>
#include <vector>

#define MAXN 100005

using namespace std;

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

int viz[MAXN];
vector <int> graph[MAXN];

inline void Read(int &N, int &M) {
    int x, y;

    fin >> N >> M;

    for (int i = 1; i <= M; i++) {
        fin >> x >> y;

        graph[x].push_back(y);
        graph[y].push_back(x);
    }
}

inline void DFS(int node) {
    viz[node] = 1;

    for (auto x : graph[node]){
        if (!viz[x])
            DFS(x);
    }
}

int main () {
    int M, N, nrc = 0;
    Read(N, M);

    for (int i = 1; i <= N; i++) {
        if (!viz[i]) {
            DFS(i);
            nrc++;
        }
    }

    fout << nrc;

    fin.close(); fout.close(); return 0;
}