Cod sursa(job #2487604)

Utilizator MithrilBratu Andrei Mithril Data 4 noiembrie 2019 23:17:35
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 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)
{
    seen[i] = 1;
    for(auto x: graph[i]) if(!seen[x]) 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;
}