Cod sursa(job #2196685)

Utilizator codrin18Diac Eugen Codrin codrin18 Data 20 aprilie 2018 08:46:04
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <algorithm>
#define NMAX 100010
using namespace std;
int n, m;
bool viz[NMAX];
vector<int>G[NMAX];
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]);
}
int main()
{
    ifstream fin("dfs.in");
    ofstream fout("dfs.out");
      int x, y;
    fin >> n >> m;
    for( int 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);
    }
     int i;
    int nrsol = 0;
    for(i = 1; i <= n; i++)
        if(!viz[i])
        {
            nrsol++;
            dfs(i);
        }
    fout << nrsol << '\n';
    return 0;
}