Cod sursa(job #2965564)

Utilizator scott7qScott Scott scott7q Data 15 ianuarie 2023 16:25:35
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream cin("dfs.in");
ofstream cout("dfs.out");
const int NLIM = 100001;
int n, m, insule;
bool vizizat[NLIM];
vector<int> Muchii[NLIM];

void DFS(int Nod)
{
    vizizat[Nod] = true;
    for (unsigned int i = 0; i < Muchii[Nod].size(); ++i)
    {
        int vecin = Muchii[Nod][i];
        if (!vizizat[vecin])
            DFS(vecin);
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        int x, y;
        cin >> x >> y;
        Muchii[x].push_back(y);
        Muchii[y].push_back(x);
    }
    for (int i = 1; i <= n; ++i)
        if (!vizizat[i])
        {
            insule += 1;
            DFS(i);
        }

    cout << insule;

    cin.close();
    cout.close();
    system("PAUSE");
    return 0;
}