Cod sursa(job #2270852)

Utilizator AtanaseTeodorAtanase Alexandru-Teodor AtanaseTeodor Data 27 octombrie 2018 17:40:43
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
#define N 100005

using namespace std;

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

vector<int>v[N];

int vf[N],conex=0;

void DFS(int x)
{
    vf[x] = 1;
    for(int i=0; i< v[x].size(); i++)
    {
        if(vf[v[x][i]] == 0)
        {
            vf[v[x][i]] = 1;
            DFS(v[x][i]);
        }
    }
}

int main()
{
    int n,m,a,b;
    fin>>n>>m;
    for(int i=0; i<m; i++)
    {
        fin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    for(int i=1; i<=n; i++)
    {
        if(vf[i]==0)
        {
            DFS(i);
            conex++;
        }
    }
    fout<<conex;
    return 0;
}