Cod sursa(job #2665938)

Utilizator paulconst1Constantinescu Paul paulconst1 Data 31 octombrie 2020 15:08:43
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream in("dfs.in");
ofstream out("dfs.out");
120
const int MAXN = 100000;
bool vis [MAXN + 1];
vector <int> adj[MAXN + 1];

void dfs(int currNode)
{
    vis[currNode] = true;
    for (int nbh: adj[currNode])
        if (!vis[nbh])
            dfs(nbh);
}

int main()
{
    int N, M;
    in >> N >> M;

    int x, y;
    for (int i = 0; i < M; ++i)
    {
        in >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    int nrComp = 0;
    for (int i = 1; i <= N; ++i)
    {
        if (!vis[i])
        {
            ++nrComp;
            dfs(i);
        }
    }

    out << nrComp;

    return 0;
}