Cod sursa(job #1108310)

Utilizator A63N7pTudor Nazarie A63N7p Data 15 februarie 2014 16:12:26
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

#define NMAX 100000

ifstream in;
ofstream out;

vector<int> graph[NMAX];
bool graphVisited[NMAX];

void dfs(int node);

int main(int argc, char *argv[])
{
    in.open("dfs.in");
    out.open("dfs.out");
    memset(graphVisited, false, sizeof(graphVisited));
    int n, m;
    in >> n >> m;
    for (int i = 0; i < m; i++) {
        int aux1, aux2;
        in >> aux1 >> aux2;
        graph[aux1].push_back(aux2);
        graph[aux2].push_back(aux1);
    }

    int connCount = 1;

    for (int i = 0; i < m; i++) {
        if (!graphVisited[i]){
            dfs(i);
            connCount++;
        }
    }
    out << connCount;
    in.close();
    out.close();
    return 0;
}

void dfs(int node)
{
    if (graphVisited[node])
        return;
    else {
        graphVisited[node] = true;
        int n = graph[node].size();
        for (int i = 0; i < n; i++)
            dfs(graph[node].at(i));
    }
}