Cod sursa(job #2487601)

Utilizator MithrilBratu Andrei Mithril Data 4 noiembrie 2019 23:15:09
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>

#define NMAX 100001

using namespace std;

vector<int> graph[NMAX];
bitset<NMAX> seen;
ifstream fin("dfs.in");
ofstream fout("dfs.out");

void dfs(int i)
{
    for(auto x: graph[i]) if(!seen[x])
    {
        seen[x] = 1;
        dfs(x);
    }
}

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

    fout << count;
    fin.close();
    fout.close();

    return 0;
}