Cod sursa(job #1964332)

Utilizator DianaVelciovVelciov Diana DianaVelciov Data 13 aprilie 2017 12:51:28
Problema Parcurgere DFS - componente conexe Scor 95
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

ifstream in("dfs.in");
ofstream out("dfs.out");

int N,M,s;
bool use[1000005];
vector <int> G[1000005];

void dfs (int node)
{
    use[node]=true;
    for (int vecin : G[node])
        if(!use[vecin])
            dfs(vecin);
}

int main()
{
    in >> N >> M;
    for (int i = 0; i < M; ++i)
    {
        int x,y;
        in >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for (int i = 1; i <= N; ++i)
        if (!use[i])
        {
            ++s;
            dfs(i);
        }
    out << s;
    return 0;
}