Cod sursa(job #2494126)

Utilizator Emi09Buciu Emilian Emi09 Data 17 noiembrie 2019 13:20:09
Problema Parcurgere DFS - componente conexe Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int DMAX = 100005;

unsigned int N, M;

bool vizitat[DMAX];

vector < int > Muchii[DMAX];

void DFS(unsigned int nod)
{
    vizitat[nod] = true;

    for (unsigned int i = 0; i < Muchii[nod].size(); i++)
    {
        unsigned int vecin = Muchii[nod][i];

        if (!vizitat[vecin])
            DFS(vecin);
    }
}

void read()
{
    unsigned int nodStart;
    unsigned int nrInsule = 0;

    f >> N >> M >> nodStart;

    vizitat[nodStart] = 1;

    for (unsigned int i = 1; i <= M; i++)
    {
        unsigned int x, y;

        f >> x >> y;

        Muchii[x].push_back(y);
        Muchii[y].push_back(x);
    }

    for (unsigned int i = 1; i <= N; i++)
        if (!vizitat[i])
        {
            nrInsule++;
            DFS(i);
        }

    g << nrInsule;
}

/*
 *  1 - 2 4
 *  2 - 1
 *  3 - 5
 *  4 - 1
 *  5 - 3
 *  6 - /
 *
 */

int main()
{
    read();

    return 0;
}