Cod sursa(job #3320859)

Utilizator c0drinn_Rau Codrin c0drinn_ Data 7 noiembrie 2025 16:56:23
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

vector <int> v[100001];
int viz[100001], c[100001];

int main()
{
   int n, m, x,y;
   fin >> n>>m;
   for(int i = 1; i <=m; i++){
     fin >> x >>y;
     v[x].push_back(y);
    v[y].push_back(x);
   }
   int nrcomp=0;
   for(int i = 1; i<=n; i++){
     if(viz[i] == 0){
       nrcomp ++;
       int u = 1, p = 1;
       c[p] = i;
       viz[i]=1;
       while(p<=u){
         int vf=c[p];
         p++;
         int l=v[vf].size();
         for(int j=0;j<l;j++){
           if(viz[v[vf][j]]==0){
             viz[v[vf][j]]=1;
             u++;
             c[u]=v[vf][j];
           }
         }
       }
     }
   }
   fout<<nrcomp;

  return 0;
}