Cod sursa(job #2903502)

Utilizator cosmin1805Iacobai Cosmin Andrei cosmin1805 Data 17 mai 2022 17:16:50
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <vector>
#include <fstream>
#include <bitset>
using namespace std;
const int N = 1e5;
vector <int> a[N + 1];
bitset<N + 1>viz;
int n;
ifstream in("dfs.in");
ofstream out("dfs.out");
void dfs(int x)
{
    viz[x] = 1;
    for (auto y : a[x]) {
        if (!viz[y]) {
            dfs(y);
        }
    }
}
int main()
{
    int m, x,nc=0;
    in >> n >> m ;
    for (int y = 0; y < m; y++)
    {
        int i, j;
        in >> i >> j;
        a[i].push_back(j);
        a[j].push_back(i);
    }
    for (int y = 1; y <= n; y++) {
        if (!viz[y]) {
            dfs(y);
            nc++;
        }    
    }
    out << nc;
    return 0;
}