Cod sursa(job #2692057)

Utilizator MattiaMattia Iojica Mattia Data 31 decembrie 2020 13:25:06
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

bitset <100005>v;
vector <int>l[100005];

int n, m, nr;

void dfs(int nod)
{
    v[nod] = 1;

    for(int i = 0; i < l[nod].size(); i++)
        if(v[l[nod][i]] == 0)
            dfs(l[nod][i]);
}

int main()
{
    f >> n >> m;

    for(int i = 1; i <= m; i++)
    {
        int x, y;
        f >> x >> y;
        l[x].push_back(y);
        l[y].push_back(x);
    }

    for(int i = 1; i <= n; i++)
        if(v[i] == 0)
        {
            dfs(i);
            nr++;
        }

    g << nr;

    return 0;
}