Cod sursa(job #2501712)

Utilizator SoranaAureliaCatrina Sorana SoranaAurelia Data 30 noiembrie 2019 09:50:29
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <cstdio>
#include <vector>
using namespace std;

vector<int>graph[100005];
int n, m, x, y;
int viz[100005];
int nr;

void dfs(int i){
    for(auto &v:graph[i])
        if(viz[v]==0){
            viz[v]=1;
            dfs(v);
        }

}
int main()
{
    freopen("dfs.in", "r", stdin);
    freopen("dfs.out", "w", stdout);
    scanf("%d %d", &n, &m);
    for(int i=1; i<=m; i++){
        scanf("%d %d", &x, &y);
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for(int i=1; i<=n; i++)
        if(viz[i]==0){
            viz[i]=1;
            dfs(i);
            nr++;
        }
    printf("%d",nr);
    return 0;
}