Cod sursa(job #1826550)

Utilizator doroftei1999Doroftei Andrei doroftei1999 Data 10 decembrie 2016 16:38:58
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#include <vector>

using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");

int n, m, nr;
bool viz[100005];
vector<int>v[100005];
 
void citire() {
    in >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        in >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
}
 
void DFS(int nod) {
    viz[nod] = true;
    for (int i = 0; i < v[nod].size(); i++) 
        if (!viz[v[nod][i]]) 
            DFS(v[nod][i]);
}   
 
int main() {
    citire();
    for (int i = 1; i <= n; i++) 
    if (!viz[i]) {
        nr++; 
        DFS(i);
    }
    out << nr;
    return 0;
}