Cod sursa(job #2270532)

Utilizator alex_bb8Banilean Alexandru-Ioan alex_bb8 Data 27 octombrie 2018 11:25:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <bits/stdc++.h>

using namespace std;

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

vector <int> G[100001];
bool viz[100001];

void DFS(int node)
{
 viz[node]=1;
 for(int j=0;j<G[node].size();++j)
 {
  int x=G[node][j];
  if(viz[x]==0)
   DFS(x);
 }
}

int main()
{
    int n,m,i,x,y,nr=0;
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
     f>>x>>y;
     G[x].push_back(y);
     G[y].push_back(x);
    }
    for(i=1;i<=n;i++)
    {
     if(viz[i]==0)
     {
      viz[i]=1;
      DFS(i);
      nr++;
     }
    }
    g<<nr;
    return 0;
}