Cod sursa(job #2468171)

Utilizator _Tudor_Tudor C _Tudor_ Data 5 octombrie 2019 13:15:56
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <vector>

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

const int NMax = 100000;

vector<int> g[NMax + 5];
int n, m;

void read()
{
    int x, y;
    fin >> n >> m;

    for(int i = 0; i < m; ++i)
    {
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

bool viz[NMax + 5];

void DFS(int x)
{
    viz[x] = true;
    for(int i = 0; i < g[x].size(); ++i)
    {
        if(!viz[g[x][i]])
            DFS(g[x][i]);
    }
}

/// fct pt list si fct pt afisare

int main()
{
    read();

    int total = 0;
    for(int i = 1; i <= n; ++i)
    {
        if(viz[i] == false)
        {
            DFS(i);
            total++;
        }
    }
    fout << total;

    return 0;
}