Cod sursa(job #1999018)

Utilizator dumitrescu_andreiDumitrescu Andrei dumitrescu_andrei Data 9 iulie 2017 23:35:16
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <bits/stdc++.h>

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

int n,m;

int viz[100001];

vector <int> V[100001];

void DFS(int x)
{
   viz[x]=1;

   for(int i=0;i<V[x].size();++i)
    if(!viz[V[x][i]])
        DFS(V[x][i]);
}

int main()
{
f>>n>>m;
for(int i=1;i<=m;++i)
{
    int x,y;
    f>>x>>y;
    V[x].push_back(y);
    V[y].push_back(x);
}
int nr=0;

for(int i=1;i<=n;++i)
   if(!viz[i])
     {
         nr++;
         DFS(i);
     }

g<<nr;
return 0;


}