Cod sursa(job #3373746)

Utilizator rucimangeac ruxandra ruci Data 7 noiembrie 2026 19:16:29
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;

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

int a[101][101];
int viz[100001];
int n;

int dfs(int x)
{
    viz[x] = 1;
    int y;
    for(y = 1; y <= n; y++)
    {
        if(a[x][y] == 1 and viz[y] == 0)
            dfs(y);
    }
}

int main()
{
    int x,y,m,i,j,cnt = 0;
    f >> n >> m;
    for(i = 1; i <= m; i++)
    {
        f >> x >> y;
        a[x][y] = 1;
        a[y][x] = 1;
    }
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            if(a[i][j] == 1)
            {
                dfs(i);
                cnt++;
            }
        }
    }
    g << cnt;
    return 0;
}