Cod sursa(job #1503916)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 17 octombrie 2015 08:34:48
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <bitset>

using namespace std;

const int maxn = 100005;

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

void dfs(int node) {
    viz[node] = 1;
    for(auto vecin : g[node])
        if(!viz[vecin])
            dfs(vecin);
}

int main()
{
    ifstream fin("dfs.in");
    ofstream fout("dfs.out");
    fin >> n >> m;
    for(int i = 1 ; i <= m ; ++ i) {
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    int comp = 0;
    for(int i = 1 ; i <= n ; ++ i)
        if(!viz[i]) {
            dfs(i);
            ++ comp;
        }
    fout << comp << '\n';
    return 0;
}