Cod sursa(job #1325062)

Utilizator hopingsteamMatraguna Mihai-Alexandru hopingsteam Data 23 ianuarie 2015 10:52:00
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
/*
    Rescriere OLI 2015
    Start: 10:45
*/

#include    <iostream>
#include    <fstream>
#include    <vector>

using namespace std;

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

const int NLIM = 100005;

int N, M, answer;
vector < int > V[NLIM];

bool beenThere[NLIM];

void DFS(int Node)
{
    beenThere[Node] = true;
    for(unsigned int i = 0; i < V[Node].size(); i++)
    {
        int Next = V[Node][i];
        if(!beenThere[Next])
            DFS(Next);
    }
}

void Read()
{
    int x, y;
    fin >> N >> M;
    for(int i = 1; i <= M; i++)
    {
        fin >> x >> y;
        V[x].push_back(y);
        V[y].push_back(x);
    }

    for(int i = 1; i <= N; i++)
    {
        if(!beenThere[i])
        {
            answer += 1;
            DFS(i);
        }
    }
}

int main()
{
    Read();
    fout << answer;
    return 0;
}