Cod sursa(job #2091719)

Utilizator ImbuzanRaduImbuzan Radu ImbuzanRadu Data 20 decembrie 2017 09:16:28
Problema Parcurgere DFS - componente conexe Scor 95
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int n, m, comp;
bool viz[1000005];
vector <int> a[1000005];

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

void dfs(int node){
    viz[node] = 1;
    for(int i = 0; i < a[node].size(); i++){
        if(viz[a[node][i]] == 0)
            dfs(a[node][i]);
    }
}

int main()
{
    int x, y;
    in >> n >> m;
    for(int i = 1; i <= m; i++){
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    for(int i = 1; i <= n; i++){
        if(viz[i] == 0){
            dfs(i);
            comp++;
        }
    }

    out << comp;
    return 0;
}