Cod sursa(job #2196569)

Utilizator stoicageorgeStoica George stoicageorge Data 19 aprilie 2018 18:55:33
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <vector>
#include <fstream>

#define NMAX 100010
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m;
bool viz[NMAX];
vector<int>G[NMAX];

void citire();
void dfs(int);

int main()
{
    int i;
    int nrsol = 0;
    citire();
    for(i = 1; i <= n; i++)
        if(!viz[i])
        {
            nrsol++;
            dfs(i);
        }
    fout << nrsol << '\n';
    return 0;
}
void citire()
{
    int i, x, y;
    fin >> n >> m;
    for(i = 0; i < m; i++)
    {
        fin >> x >> y;
        //adaugam x in lista de adiacenta a lui y;
        G[y].push_back(x);
        G[x].push_back(y);
    }
}

void dfs(int x)
{
    int i;
   // fout << x << ' ';
    viz[x] = 1;
    for(i = 0; i < G[x].size(); i++)
        if(!viz[G[x][i]])
            dfs(G[x][i]);
}