Cod sursa(job #1464249)

Utilizator dex4Darjan Catalin dex4 Data 22 iulie 2015 18:33:08
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 100005

using namespace std;

int nrNodes, nrPairs, x, y, nrComps;
bool nCheck[nmax];
vector <int> graph[nmax];

void build(){
    ifstream f("dfs.in");
    f >> nrNodes >> nrPairs;
    for(int i=1; i<=nrPairs; i++){
        f >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
}

void dfs(int x){
    nCheck[x] = true;
    for(int i=0; i<graph[x].size(); i++){
        int y = graph[x][i];
        if(!nCheck[y])
            dfs(y);
    }
}

int main()
{
    build();
    for(int i=1; i<=nrNodes; i++)
        if(!nCheck[i]){
            nrComps++;
            dfs(i);
        }
    ofstream g("dfs.out");
    g << nrComps << "\n";
    return 0;
}