Cod sursa(job #3212470)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 11 martie 2024 19:37:32
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x << ": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "dfs";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

int n, m;
vector<int> g[100005];
int d[100005];
bitset<100005> viz;
void dfs(int nod)
{
    viz[nod] = 1;
    for (auto i : g[nod])
        if (!viz[i])
        {
            dfs(i);
        }
}

int main()
{
    fin >> n >> m;
    while (m--)
    {
        int x, y;
        fin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }
    int nrc = 0;
    for (int i = 1; i <= n; ++i)
        if (!viz[i])
        {
            dfs(i);
            nrc++;
        }
    fout << nrc << '\n';

    return 0;
}