Cod sursa(job #3368399)

Utilizator maria_avramAvram Maria Teodora maria_avram Data 14 octombrie 2026 13:28:11
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb

#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

int no_cc;
constexpr int NMAX = 100005;
vector<vector<int>>graph;
bitset<NMAX>used;
void dfs(int node)
{
    used[node]=1;
    for(int next:graph[node])
        if(!used[next])
            dfs(next);
}
int main() {
    int n,m;
    f >> n >> m;
    graph.assign(n + 1,vector<int>());
    for(int i=1; i<=m; i++)
    {
        int x, y;
        f>>x>>y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for(int i=1; i<=n; i++)
        if(!used[i])
        {
            no_cc++;
            dfs(i);
        }
    g<<no_cc;

    return 0;
}