Cod sursa(job #3214860)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 14 martie 2024 15:13:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

const int max_size = 1e5 + 20;

int viz[max_size];
vector <int> mc[max_size];

void dfs (int nod)
{
    viz[nod] = 1;
    for (auto f : mc[nod])
    {
        if (viz[f] == 1)
        {
            continue;
        }
        dfs(f);
    }
}

void solve ()
{
    int n, m;
    cin >> n >> m;
    int ans = 0;
    while (m--)
    {
        int x, y;
        cin >> x >> y;
        mc[x].push_back(y);
        mc[y].push_back(x);
    }
    for (int i = 1; i <= n; i++)
    {
        if (viz[i] == 0)
        {
            ans++;
            dfs(i);
        }
    }
    cout << ans;
    cout << '\n';
}

signed main ()
{
#ifdef LOCAL
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#else
    freopen("dfs.in", "r", stdin);
    freopen("dfs.out", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long tt;
    //cin >> tt;
    tt = 1;
    while (tt--)
    {
        solve();
    }
    return 0;
}