Cod sursa(job #2105875)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 14 ianuarie 2018 15:07:55
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <iostream>
#include <fstream>
#include <vector>
#define NMax 100005
using namespace std;

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

vector <int> v[NMax];
int n, m, viz[NMax], x, y, ans;

void dfs(int nod)
{
    viz[nod] = 1;
    for(int i : v[nod])
        if(!viz[i]) dfs(i);
}

int main()
{
    f >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i = 1; i <= n; i++)
        if(!viz[i]){
            ans++;
            dfs(i);
        }
    g << ans << '\n';
    f.close();
    g.close();
    return 0;
}