Cod sursa(job #2839607)

Utilizator Humaru_AndreiHumaru Andrei Humaru_Andrei Data 26 ianuarie 2022 11:10:48
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
///De pe InfoArena
#include <fstream>
#include <vector>
#define NMAX 100004
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
void citire();
void DFS(int x);
int n,nrc,m;
vector <int> A[NMAX];
bool viz[NMAX];
int main()
{int i;
    citire();
    for (i = 1; i <= n; i++)
        if (!viz[i])
    {
        ++nrc;
        DFS(i);
    }
    fout << nrc << '\n';
    return 0;
}
void citire()
{int x,y,i;
    fin >> n >> m;
    for (i = 1;  i <= m; i++)
    {
        fin >> x >> y;
        A[x].push_back(y);
        A[y].push_back(x);
    }
}
void DFS(int x)
{
    int i;
    viz[x] = 1;
    for (i = 0; i < A[x].size(); i++)
        if(!viz[A[x][i]])
        DFS(A[x][i]);
}