Cod sursa(job #2658855)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 15 octombrie 2020 12:12:31
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <bits/stdc++.h>
#define nmax 100007

using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");

int n, m;
int viz[nmax];
vector<int> L[nmax];
int cc;

void Citire()
{
    int x, y;
    fin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
}

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

int main()
{
    Citire();
    for (int i = 1; i <= n; i++)
        if (viz[i] == 0)
        {
            cc++;
            DFS(i);
        }
    fout << cc << "\n";
    return 0;
}