Cod sursa(job #2979790)

Utilizator AndreiN96Andrei Nicula AndreiN96 Data 15 februarie 2023 21:15:48
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#include <vector>

using namespace std;

const int N = 100000;
vector <int> la[N + 1];
bool viz[N + 1];

void dfs(int a)
{
    viz[a] = true;
    for (auto urm: la[a])
    {
        if (!viz[urm])
        {
            dfs(urm);
        }
    }
}
int main()
{
    ifstream in("dfs.in");
    ofstream out("dfs.out");

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

    int cc = 0;
    for (int i = 1; i <= n; i ++)
    {
        if (!viz[i])
        {
            dfs(i);
            cc ++;
        }
    }

    out << cc;

    in.close();
    out.close();

    return 0;
}