Cod sursa(job #2885196)

Utilizator cristina-criCristina cristina-cri Data 5 aprilie 2022 17:55:37
Problema Parcurgere DFS - componente conexe Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

int v, e;
int x, y;
vector<int> graph[100005];
int viz[100005];

void bfs(int vf)
{
    viz[vf]=1;
    for(auto& vec:graph[vf])
    {
        if(viz[vec] == 0)
            bfs(vec);
    }
}

int main() {

    f>>v>>e;
    for(int i=1; i<=v; i++)
    {
        f>>x>>y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    int nr=0;
    for(int i=1; i<=v; i++)
    {
        if(viz[i] == 0)
        {
            bfs(i);
            nr++;
        }
    }
    g<<nr;
    return 0;
}