Cod sursa(job #2194269)

Utilizator sichetpaulSichet Paul sichetpaul Data 12 aprilie 2018 18:38:14
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>
#include <vector>
#define DIM 100001
using namespace std;
vector <int> v[DIM];
bool a[DIM];
void DFS(int i) {
   a[i]=1;
   for (int j=0;j<v[i].size();++j)
      if (!a[v[i][j]]) DFS(v[i][j]);
}
int main()
{  int n,m,i,x,y,sol=0;
    ifstream f("dfs.in");
    ofstream g("dfs.out");
    f>>n>>m;
    for (i=1;i<=m;++i) {
        f>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for (i=1;i<=n;++i)
        if (!a[i]) DFS(i),++sol;
    g<<sol;
    return 0;
}