Cod sursa(job #3361172)

Utilizator EricDimiCismaru Eric-Dimitrie EricDimi Data 21 iulie 2026 14:21:35
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

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

const int MAX_N = 100000;

vector<int> adj[MAX_N + 1];
bitset<MAX_N + 1> vis;
int n, m, p;

void Read()
{
    f >> n >> m;
    while(m--)
    {
        int x, y;
        f >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
}

void DFS(int node)
{
    vis[node] = true;
    for(int next : adj[node])
        if(!vis[next])
            DFS(next);
}

void CountComp()
{
    p = 0;
    for(int i = 1; i <= n; i++)
        if(!vis[i])
        {
            DFS(i);
            p++;
        }
}

int main()
{
    Read();
    CountComp();
    g << p << '\n';

    f.close();
    g.close();

    return 0;
}