Cod sursa(job #2360314)

Utilizator GabyD002Dobrita Gabriel GabyD002 Data 1 martie 2019 18:22:36
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <fstream>
#include <vector>
#define NM 100005
#define pb push_back
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,m,nrc;
bool viz[NM];
vector <int> v[NM];
void DFS(int nod)
{   viz[nod]=1;
    for(unsigned int j=0; j<v[nod].size(); j++)
        if(!viz[v[nod][j]]) DFS(v[nod][j]);
}
int main()
{   f>>n>>m;
    while(m--)
    {   int x,y;
        f>>x>>y;
        v[x].pb(y);
        v[y].pb(x);
    }
    for(int i=1; i<=n; i++)
        if(!viz[i])
        {   DFS(i);
            nrc++;
        }
    g<<nrc;
    return 0;
}