Cod sursa(job #1567227)

Utilizator CovrigBogdanBogdan Covrig CovrigBogdan Data 12 ianuarie 2016 23:43:01
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
using namespace std;

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

const int nmax = 100050;
vector <int> graf[nmax];
int n, m, counter;
bool checked[nmax];

void dfs (int node){
    checked[node] = true;
    for(int i = 0; i < graf[node].size(); i++)
        if(!checked[graf[node][i]])
            dfs(graf[node][i]);
}

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

    for(int i = 1; i <= n; i++){
        if(!checked[i]){
            dfs(i);
            ++counter;
        }
    }
    out << counter << '\n';
    return 0;
}