Cod sursa(job #2669275)

Utilizator bogdan2604Bogdan Dumitrescu bogdan2604 Data 6 noiembrie 2020 17:34:33
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 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 <vector<int>> mat;
bitset <100001> vf;

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

int main()
{
    f >> n >> m;
    mat.resize(n + 1);
    while(m -- )
    {
        f >> x >> y;
        mat[x].push_back(y);
    }
    for(int i = 1; i <= n; ++ i)
        if(!vf[i])
    {
        ++ componente;
        vf[i] = 1;
        DFS(i);
    }
    g << componente;
}