Cod sursa(job #1166340)

Utilizator Dddarius95Darius-Florentin Neatu Dddarius95 Data 3 aprilie 2014 14:44:38
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>
#include <vector>
#define Nmax 100009
#define pb push_back
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");

int N,M,sol,x,y,comp[Nmax];
vector < int > G[Nmax];


inline void DFS(int node)
{
    comp[node]=sol;
    for(vector < int > :: iterator it=G[node].begin();it!=G[node].end();++it)
        if(!comp[*it])DFS(*it);
}

int main()
{
    f>>N>>M;
    for (int i=1; i<=M; ++i)
        f>>x>>y , G[x].pb(y) , G[y].pb(x);
    for (int i=1; i<=N; ++i)
      if (!comp[i])
            ++sol , DFS(i);
    g<<sol<<'\n';
    f.close(); g.close();
    return 0;
}