Cod sursa(job #2342506)

Utilizator SamySam2099Irsai Samuel SamySam2099 Data 12 februarie 2019 21:14:01
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
#define Nmax 100005
vector <int> G[Nmax];

int n,m,c;
bool viz[Nmax];
void read()
{
   f>>n>>m;
   for(int i=1;i<=m;i++)
   {
       int x,y;
       f>>x>>y;
       G[x].push_back(y);
       G[y].push_back(x);
   }

}
void dfs(int nod)
{
    viz[nod]=true;
    for(int i=0;i<G[nod].size();i++)
    {
        int vecin=G[nod][i];
        if(!viz[vecin])
           dfs(vecin);
    }
}
int main()
{
    read();
    for(int i=1;i<=n;i++)
    {
        if(!viz[i]) dfs(i),c++;
    }
    g<<c;
    return 0;
}