Cod sursa(job #3172460)

Utilizator Razvan23Razvan Mosanu Razvan23 Data 20 noiembrie 2023 18:04:58
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>
using namespace std;

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

bitset<100005> v;
vector<int> a[100005];
int n, m, nrc;

void dfs(int i)
{
    v[i] = 1;
    for(auto w : a[i])
        if(v[w] == 0) dfs(w);
}

int main()
{
    int i, x, y;
    fin >> n >> m;
    for(i=1; i<=m; i++)
    {
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for(i=1; i<=n; i++)
        if(v[i] == 0)
        {
            nrc++;
            dfs(i);
        }
    fout << nrc;
    fin.close();
    fout.close();
    return 0;
}