Cod sursa(job #2486228)

Utilizator Fantastic_Mantudor voicu Fantastic_Man Data 2 noiembrie 2019 15:20:07
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#include <vector>
using namespace std;
vector <int> g[100001];
bool f[100001];
int n,m,ans,a,b;
void dfs(int nod) {
    f[nod]=true;
    for(int i=0;i<g[nod].size();i++) {
        int x=g[nod][i];
        if(!f[x])
            dfs(x);
    }
}
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int main() {
    fin>>n>>m;
    for(int i=0;i<m;i++) {
        fin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i=1;i<=n;i++)
        if(!f[i]) {
            dfs(i);
            ans++;
        }
    fout<<ans;

    return 0;
}