Cod sursa(job #3339016)

Utilizator serban__georgeSerban George serban__george Data 5 februarie 2026 18:39:19
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>
using namespace std;

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

bool viz[101];
bool a[101][101];
int n, m;

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

int main()
{
    f >> n >> m;

    for(int k = 1; k <= m; k++)
    {
        int x, y;
        f >> x >> y;
        a[x][y] = a[y][x] = 1;
    }

    int nrc = 0;
    for(int i = 1; i <= n; i++)
        if(!viz[i])
            DFS(i), nrc++;

    g << nrc;
    return 0;
}