Cod sursa(job #2282292)

Utilizator Seba951Sebastian Boerescu Seba951 Data 13 noiembrie 2018 16:20:44
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int N=100001;
vector<int> a[N];
int viz[N];
int n,m,p;

void citire()
{
    in>>n>>m;
    int x,y;
    for(int i=1; i<=m; ++i)
    {
        in>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    in.close();
}

void dfs(int x)
{
    viz[x]=true;
    for(auto y : a[x])
        if(!viz[y]) dfs(y);
}

int main()
{
    int nr=0;
    citire();
    for(int i=1; i<=n; ++i)
        if(viz[i]==false)
        {
            dfs(i);
            ++nr;
        }
    out<<nr;
    return 0;
}