Cod sursa(job #2662499)

Utilizator Pop_Rares123Pop Rares Pop_Rares123 Data 24 octombrie 2020 10:45:07
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 105

using namespace std;

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

bool ap[100005];
int n, m, x, y;
vector <int> nod[100005];

void read()
{
    fin >> n >> m;

    for (int i = 0; i < m; ++ i) {
        fin >> x >> y;
        nod[x].push_back(y);
        nod[y].push_back(x);
    }
}

void dfs(int node)
{
    ap[node] = 1;

    for (int i = 0; i < nod[node].size(); ++ i) {
        if (!ap[nod[node][i]]) {
            dfs(nod[node][i]);
        }
    }
}

int main()
{
    read();
    int k=0;

    for(int i=1;i<=n;i++)
        if(!ap[i]){
            dfs(i);
            k++;
        }

    fout << k;
    return 0;
}