Cod sursa(job #2655927)

Utilizator RNedelcuNedelcu Radu RNedelcu Data 6 octombrie 2020 09:11:10
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
ifstream in("dfs.in");
ofstream out("dfs.out");
int N,M,sol=0;
vector<int> A[MAX];
bool viz[MAX]={0};
void DFS(int nod)
{
    viz[nod] = true;
    for(auto temp:A[nod])
        if(!viz[temp])
            DFS(temp);
}
int main()
{
    in >> N >> M;
    int x, y;
    for(int i = 1; i <= M; i++) { // Lista de adiacenta
        in >> x >> y;
        A[x].push_back(y);
        A[y].push_back(x);
    }

    for(int i = 1; i <= N; i++)
        if(!viz[i]) {
            DFS(i);
            sol++; //Crestem nr de componente conexe
        }
    out << sol;
    return 0;
}