Cod sursa(job #2658407)

Utilizator DeliaGhergheGherghe Ioana-Delia DeliaGherghe Data 13 octombrie 2020 21:25:59
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
int N, M;
vector <int> l[100001];
int viz[100001];

void DFS(int nod)
{
    viz[nod] = 1;
    for (int i = 0; i < l[nod].size(); i++)
        if (viz[l[nod][i]] == 0)
            DFS(l[nod][i]);
}

int main()
{
    ifstream fin("dfs.in");
    ofstream fout("dfs.out");

    fin >> N >> M;
    int i, j, k;

    for (i = 0; i < M; i++)
    {
        fin >> j >> k;
        l[j].push_back(k);
        l[k].push_back(j);
    }

    int rez = 0;

    for (i = 1; i <= N; i++)
        if (viz[i] == 0)
    {
        rez++;
        DFS(i);
    }

    fout << rez;

    fin.close();
    fout.close();

    return 0;
}