Cod sursa(job #2544487)

Utilizator MarcGrecMarc Grec MarcGrec Data 12 februarie 2020 09:47:45
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#define MAX_N 100000

#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

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

int n, m;
vector<int> G[MAX_N + 1];
bitset<MAX_N + 1> V;

void Dfs(int nod);

int main()
{
    fin >> n >> m;

    for (int i = 0, x, y; i < m; ++i)
    {
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    int rasp = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (!V[i])
        {
            ++rasp;
            Dfs(i);
        }
    }

    fout << rasp;

    fin.close();
    fout.close();
    return 0;
}

void Dfs(int nod)
{
    V[nod] = true;
    for (int vecin : G[nod])
    {
        if (!V[vecin])
        {
            Dfs(vecin);
        }
    }
}