Cod sursa(job #2355946)

Utilizator andreeacristianaAlbu Andreea-Cristiana andreeacristiana Data 26 februarie 2019 13:33:48
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int viz[100003];
vector <int> graph[100003];
void DFS(int nod)
{
    viz[nod] = 1;
    int lim = graph[nod].size();

    for(int i = 0; i < lim; i++)
        if(viz[graph[nod][i]] == 0)
            DFS(graph[nod][i]);
}
int main()
{
    int n, m, a, b, i, componente_conexe = 0;
    f>> n >> m;

    for(i = 1; i <= m; i++)
    {
        f >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    for(i = 1; i <= n; i++)
        if(viz[i] == 0)
        {
            componente_conexe++;
            DFS(i);
        }

    g << componente_conexe;
    return 0;
}