Cod sursa(job #3255518)

Utilizator raulthestormIlie Raul Ionut raulthestorm Data 10 noiembrie 2024 20:32:38
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
#include <set>
/// Varianta 2
/// set din STL
using namespace std;
const int NMAX = 100001;

int n;
bool viz[NMAX];
set<int> L[NMAX];

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

void citire()
{
    int m, x, y;
    f >> n >> m;
    while(m--)
    {
        f >> x >> y;
        L[x].insert(y);
        L[y].insert(x);
    }
}

void DFS(int i)
{
    viz[i] = 1;
    for(auto nod : L[i])
        if(!viz[nod])
            DFS(nod);
}

int main()
{
    int nrCC = 0;
    citire();
    for(int i = 1; i <= n; i++)
        if(!viz[i])
        {
            nrCC++;
            DFS(i);
        }
    g << nrCC;
    f.close();
    g.close();
    return 0;
}