Cod sursa(job #3251737)

Utilizator TonyyAntonie Danoiu Tonyy Data 26 octombrie 2024 18:41:38
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

ifstream fin ("dfs.in");
ofstream fout("dfs.out");

const int Max = 1e5 + 1;

vector<int> graph[Max];
bitset<Max> v;

void dfs(int node)
{
    v[node] = 1;
    for(int i: graph[node])
        if(!v[i])
            dfs(i);
}

int main()
{
    int n, m, c = 0;
    fin >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        int x, y;
        fin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for(int i = 1; i <= n; ++i)
        if(!v[i])
        {
            dfs(i);
            ++c;
        }
    fout << c;

    fin.close();
    fout.close();
    return 0;
}