Cod sursa(job #2671809)

Utilizator robertnanu_fmiNanu Robert-Ionut robertnanu_fmi Data 12 noiembrie 2020 18:24:23
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

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

int n,m,x,y,componente;

vector <int> mat[100003];
bool viz[100001];

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

int main()
{
    f >> n >> m;
    while(m -- )
    {
        f >> x >> y;
        mat[x].push_back(y);
        mat[y].push_back(x);
    }

    for(int i = 1; i <= n; ++ i)
        if(!viz[i])
        {
            ++ componente;
            viz[i] = 1;
            DFS(i);
        }
    g << componente;
}