Cod sursa(job #1198036)

Utilizator hopingsteamMatraguna Mihai-Alexandru hopingsteam Data 14 iunie 2014 12:38:19
Problema Parcurgere DFS - componente conexe Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
/*
 * Includes
 */

#include    <fstream>
#include    <vector>

using namespace std;

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

/*
 * Limits
 */

#define NLIM 100000

/*
 * Variables
 */
 
int N, M, answer;
vector  < int > V[NLIM];
bool beenThere[NLIM];

/*
 * Here we go
 */
 
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);
    }
}

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

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

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