Cod sursa(job #2818937)

Utilizator bogikanagyNagy Boglarka bogikanagy Data 17 decembrie 2021 13:50:16
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <vector>

#define ll long long

using namespace std;

ifstream cin ("dfs.in");
ofstream cout ("dfs.out");

struct element
{
    bool seen=false;
    vector <ll> sz;
};
vector <element> x;

ll n,m,i,a,b;

void dfs (ll act)
{
    x[act].seen=true;
    for (auto e:x[act].sz)
    {
        if (!x[e].seen) dfs(e);
    }
}

int main()
{
    cin>>n>>m;
    x.resize(n+1);
    for (i=1;i<=m;++i)
    {
        cin>>a>>b;
        x[a].sz.push_back(b);
        x[b].sz.push_back(a);
    }

    ll db=0;
    for (i=1;i<=n;++i)
    {
        if (!x[i].seen)
        {
            db++;
            dfs(i);
           // break;
        }
    }


   // for (i=1;i<=n;++i) if (x[i].seen) db++;
    cout<<db<<"\n";
    return 0;
}