Cod sursa(job #3338137)

Utilizator Luca_ManguciManguci Luca-George Luca_Manguci Data 31 ianuarie 2026 19:45:10
Problema Parcurgere DFS - componente conexe Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>

using namespace std;

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

int n;
bool A[101][101], viz[101];

void citire()
{
    int m, x, y;
    f >> n >> m;
    while (m--)
    {
        f >> x >> y;
        A[x][y] = A[y][x] = 1;
    }
}

void DFS(int nod)
{
    viz[nod] = 1;
    for (int i = 1; i <= n; i++)
        if (A[nod][i] && !viz[i])
            DFS(i);
}

int main()
{
    int nrCC = 0;
    citire();

    for (int i = 1; i <= n; i++)
        if (!viz[i])
        {
            nrCC++;
            DFS(i);
        }

    g << nrCC;

    f.close();
    g.close();
    return 0;
}