Cod sursa(job #2168620)

Utilizator rangal3Tudor Anastasiei rangal3 Data 14 martie 2018 11:45:39
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include <fstream>
#include <bitset>
#include <vector>
#define N 100003

using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

bitset<N> f;
vector<int> g[N];
int n,m,s;
int nrc;

int x,y;

inline void DFS(int nod)
{
    f[nod] = 1;
    for(auto i : g[nod])
        if(!f[i])
            DFS(i);
}

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

    for(int i=1; i<=n; ++i)
        if(!f[i])
            DFS(i), ++nrc;

    fout<<nrc;

    return 0;
}