Cod sursa(job #2345002)

Utilizator Alex221Dumitru Alexandru Alex221 Data 15 februarie 2019 19:48:13
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.49 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m,x,y,nr;
bool viz[100001];
vector<int> a[100001];
void dfs(int c)
{ viz[c]=1;
   for(int k=0;k<a[c].size();k++)
   { if(viz[a[c][k]]==0)
       dfs(a[c][k]);
   }
}
int main()
{ f>>n>>m;
  for(int i=1;i<=m;i++)
  { f>>x>>y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  for(int i=1;i<=n;i++)
    if(viz[i]==0)
  { nr++;
       dfs(i);
  }
  g<<nr;
    return 0;
}