Cod sursa(job #2718784)

Utilizator alexia208160Popescu Alexia Maria alexia208160 Data 9 martie 2021 10:14:31
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

bool visit[100001];

vector <int> v[100001];

void DFS(int x)
{
    for(int i = 0; i < v[x].size(); i++)
        if(visit[v[x][i]] == 0)
        {
            DFS(v[x][i]);
            visit[v[x][i]] = 1;
        }

}

int main()
{
    int n, m, ct = 0, x, y;
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i = 1; i <= n; i++)
    {
        if(visit[i] == 0)
        {
            ct++;
            DFS(i);
        }
    }
    fout << ct;
    return 0;
}