Cod sursa(job #947419)

Utilizator BitOneSAlexandru BitOne Data 7 mai 2013 14:07:05
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <vector>
#include <cstdlib>
#include <fstream>

using namespace std;

const int NMAX = 100011;

bool was[NMAX];
vector<int> G[NMAX];

inline void DFS(int x)
{
    was[x] = true;
    for(auto& y : G[x])
    {
        if(!was[y])
        {
            DFS(y);
        }
    }
}
int main()
{
    int N, M, x, y, count;
    ifstream in("dfs.in");
    ofstream out("dfs.out");

    for(in >> N >> M; M; --M)
    {
        in >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    count = 0;
    for(x = 1; x <= N; ++x)
    {
        if(!was[x])
        {
            ++count;
            DFS(x);
        }
    }
    out << count << '\n';
}