Cod sursa(job #2288927)

Utilizator victorv88Veltan Victor victorv88 Data 24 noiembrie 2018 09:41:22
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

vector<int>graph[100005];

ifstream f("dfs.in");
ofstream g("dfs.out");

int n, m, s,from, to,viz[100005],nr;

void bfs(int ind)
{
    viz[ind]=1;
    for (auto el:graph[ind])
    {
        if (viz[el]==0)
            bfs(el);
    }
}

int main() {
    f >> n >> m;
    for (int i=1; i<=m; i++)
    {
        f >> from >> to;
        graph[from].push_back(to);
        graph[to].push_back(from);
    }
    for (int i=1; i<=n; i++)
    {
        if (!viz[i])
        {
            nr++;
            bfs(i);
        }
    }
    cout << nr;
    return 0;
}