Cod sursa(job #2246161)

Utilizator Ioana_AndreeaCristescu Ioana Ioana_Andreea Data 26 septembrie 2018 18:47:43
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
#include <vector>
using namespace std;

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

const int NMax = 100000;
int N,M,Contor;
vector <int> G[NMax + 5];
bool Use[NMax + 5];

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

void DFS(int Nod)
{
    Use[Nod] = 1;
    for(int i = 0; i < (int)G[Nod].size(); ++i)
        {
            int Vecin = G[Nod][i];
            if(!Use[Vecin])
                DFS(Vecin);
        }
}

int main()
{
    Read();
    for(int i = 1; i <= N; ++i)
        if (!Use[i])
        {
            DFS(i);
            Contor++;
        }
    fout << Contor << "\n";
    return 0;
}