Cod sursa(job #2270552)

Utilizator teotironTiron Teodor teotiron Data 27 octombrie 2018 11:31:29
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

unsigned N, M;
vector<unsigned> G[100003];
unsigned vz[100003];

void DFS(unsigned node)
{
    unsigned i;
    vz[node]=1;
    for(i=0; i<G[node].size(); i++)
        if(vz[G[node][i]]==0)
            DFS(G[node][i]);
}

int main()
{
    unsigned i, x, y, nr;
    nr=0;
    ifstream f("dfs.in");
    ofstream fout("dfs.out");
    f>>N>>M;
    for(i=0; i<M; i++)
    {
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(i=1; i<=N; i++)
    {
        if(vz[i]==0)
        {
            nr++;
            DFS(i);
        }
    }
    fout<<nr;
}