Cod sursa(job #2903524)

Utilizator Alexandra_s29Alexandra Stroiu Alexandra_s29 Data 17 mai 2022 17:35:47
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
///Sa se determine numarul componentelor conexe ale grafului.
#include <fstream>
#include <bitset>
#include <vector>

using namespace std;

const int N = 1e5;///100 000

bitset <N + 1> viz;
vector <int> a[N + 1];

void dfs(int x)
{
    viz[x] = 1;
    for(auto y: a[x])
    {
        if(!viz[y])
        {
            dfs(y);
        }
    }
}

int main()
{
    ifstream in("dfs.in");
    ofstream out("dfs.out");
    int n , m , nr = 0;
    in >> n >> m;
    for(int i = 0; i < m; i++)
    {
        int x , y;
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    in.close();
    for(int i = 1; i <= n; i++)
    {
        if(!viz[i])
        {
            nr++;
            dfs(i);
        }
    }
    out << nr;
    out.close();
    return 0;
}