Cod sursa(job #2055850)

Utilizator papinub2Papa Valentin papinub2 Data 3 noiembrie 2017 20:24:58
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

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

void DFS (int P)
{
    viz[P] = 1;

    for (int i = 0; i < A[P].size(); i++)
        if (!viz[A[P][i]])
            DFS(A[P][i]);
}

int main()
{
    in >> n >> m;

    for (int i = 1; i <= m; i++)
    {
        in >> x >> y;
        A[x].push_back(y);
        A[y].push_back(x);
    }

    for (int i = 1; i <= n; i++)
        if (!viz[i])
        {
            nr++;
            DFS(i);
        }

    out << nr;

    return 0;
}