Cod sursa(job #2661772)

Utilizator speedypleathGheorghe Andrei speedypleath Data 22 octombrie 2020 18:10:24
Problema Parcurgere DFS - componente conexe Scor 75
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
int n,m,a,b;
map<int, vector<int>> graf;
bool viz[50005] = {0};
void dfs(int x)
{
    viz[x] = 1;
    for(auto it:graf[x])
        if(!viz[it])
            dfs(it);
}
int main()
{
    int c = 0;
    in>>n>>m;
    for(int i=0;i<m;i++){
        in>>a>>b;
        graf[a].emplace_back(b);
        graf[b].emplace_back(a);
    }
    for(int i=1;i<=n;i++)
        if(!viz[i]){
            dfs(i);
            c++;
        }
    out<<c;
    return 0;
}