Cod sursa(job #2550099)

Utilizator PatriciaCretoiuCretoiu Patricia PatriciaCretoiu Data 18 februarie 2020 13:53:29
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("text.in");
ofstream out("text.out");
const int nmax=1e5+5;
int N, M, X, i, x, y, comp;
vector<int>G[nmax];
bitset<nmax>viz;

void DFS(int nod)
{
    viz[nod]=1;
    vector<int>:: iterator it;
    for(it=G[nod].begin(); it<G[nod].end(); it++)
        if(!viz[*it]) DFS(*it);
}

int main()
{
    in >> N >> M;
    for(i=1; i<=M; i++)
    {
        in >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for(i=1; i<=N; i++)
        if(!viz[i])
    {
        DFS(i);
        comp++;
    }
    out << comp;
    return 0;
}