Cod sursa(job #2191006)

Utilizator Narvik13Razvan Roatis Narvik13 Data 1 aprilie 2018 12:24:47
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
#include <bitset>
#define NMAX 100002

using namespace std;

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

int n,m;
vector <int> g[NMAX];
bitset <NMAX> viz;

void input()
{
    f >> n >> m;
    int x,y;
    for(int i = 1; i <= m; ++i)
    {
        f >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

void dfs(int nod)
{
    viz.set(nod);

    for(auto i : g[nod])
        if(not viz.test(i))
            dfs(i);
}

int main()
{
    input();
    int t = 0;
    for(int i = 1 ; i <= n; ++i)
        if(not viz.test(i))
        {
            ++ t;
            dfs(i);
        }
    o << t;
    return 0;
}