Cod sursa(job #2668628)

Utilizator gunther41Ionut Buzamat Alexandru gunther41 Data 5 noiembrie 2020 05:46:43
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f ("dfs.in");
ofstream o ("dfs.out");
int viz[100005];
int n, m;
vector <int> a [100005];
void dfs (int nod)
{
    viz[nod] = 1;
    for (int i : a[nod])
        if (!viz[i])
            dfs (i);
}
int main ()
{
    int conexe=0,x,y,i;
    f >> n >> m;
    for (i = 1; i <= m; i++)
    {
        f >> x >> y;
        a[x].push_back (y);
        a[y].push_back (x);
    }
    for (i = 1; i <= n; i++)
        if (!viz[i])
        {
            conexe++;
            dfs (i);
        }
    o << conexe;
}