Cod sursa(job #2986782)

Utilizator cristiana_cocheciCocheci Cristiana cristiana_cocheci Data 1 martie 2023 10:30:34
Problema Parcurgere DFS - componente conexe Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int n,m,insule;
const int NMAX = 10005;
vector <int> muchii[NMAX];
bool vizitat[NMAX];

/*
1: 2, 4
2:1
3:5
4:1
5:3
6: -
*/
void DFS(int x)
{
    vizitat[x]=1;
    for(unsigned int i=0; i< muchii[x].size();i++)
    {
        int vecin= muchii[x][i];
        if(!vizitat[vecin])
        {
            DFS(vecin);
        }
    }
}

void citire()
{
    fin >> n >> m;
    for (int i =1; i<=n;i++)
    {
        int x,y;
        fin >>x >>y;
        muchii[x].push_back(y);
        muchii[y].push_back(x);
        //legatura
    }
    for (int i=1;i<=n;i++)
    {
        if(!vizitat[i])
        {
            DFS(i);
            insule++;
        }
    }
}

int main()
{
    citire();
    fout << insule;
    return 0;
}