Cod sursa(job #2365463)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 4 martie 2019 13:54:04
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 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 i, x, y;
    fin >> n >> m;
    for(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 w : L[nod])
        if (viz[w] == 0)
        DFS(w);
}

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