Cod sursa(job #3121658)

Utilizator AlexAlAxAxelAlAx AlAx AlexAlAxAxel Data 14 aprilie 2023 17:12:30
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
using namespace std;

#define NLIM 100005

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

bool check[NLIM];
vector<int> adj[NLIM];

void DFS(int x)
{
    check[x] = true;

    if (adj[x].empty())
        return;

    for (int i = 0; i <= adj[x].size() - 1; i++)
    {
        int neigh = adj[x][i];
        if (!check[neigh])
            DFS(neigh);
    }
}

int main()
{
    int n, m, nr = 0;
    f >> n >> m;
    for (int i = 1, x, y;i <= m;i++)
    {
        f >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    for (int i = 1;i <= n;i++)
        if (!check[i])
        {
            nr++;
            DFS(i);
        }

    g << nr;

    return 0;
}