Cod sursa(job #1643110)

Utilizator roxannemafteiuMafteiu-Scai Roxana roxannemafteiu Data 9 martie 2016 17:37:06
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

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

const int N_MAX = 100001;

int N, M, conexe;
vector<int> G[N_MAX];
bitset<N_MAX> use;

void read()
{
    fin >> N >> M;
    for(int i = 0; i < M; ++i)
    {
        int x, y;
        fin >> x >> y;
        G[x].emplace_back(y);
        G[y].emplace_back(x);
    }
}

void DFS(int node)
{
    use.set(node);
    for(int son : G[node])
        if(!use[son])
            DFS(son);
}

void solve()
{
    for(int i = 1; i <= N; ++i)
        if(!use[i])
        {
            conexe++;
            DFS(i);
        }

    fout << conexe << "\n";
}

int main()
{
    read();
    solve();

    return 0;
}