Cod sursa(job #2455052)

Utilizator teodorgTeodor G teodorg Data 10 septembrie 2019 18:09:40
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int N = 100010;
int n,m,nrComponente;
vector<int> v[N];
bitset<N> viz;
void dfs(int);
int main()
{
    f>>n>>m;
    for(; m; m--)
    {
        int x,y;
        f>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1; i<=n; i++)
        if(!viz[i])
        {
            nrComponente++;
            dfs(i);
        }
    g<<nrComponente<<'\n';
    return 0;
}
void dfs(int nod)
{
    viz[nod]=1;
    for(auto vec:v[nod])
        if(!viz[vec])
            dfs(vec);
}