Cod sursa(job #1760016)

Utilizator TodeTodeAlexandru Toderica TodeTode Data 20 septembrie 2016 09:25:57
Problema Parcurgere DFS - componente conexe Scor 45
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
#define NMax 100000

vector <int> a[NMax];
bool uz[100005];
int n;

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

void Citire()
{
    int x, y;
    fin >> n;
    while(fin >> x >> y)
    {
        a[x].push_back(y);
        a[y].push_back(x);
    }
}

void DFS(int x)
{
    uz[x] = 1;
    for(int i = 0; i < a[x].size(); i++)
        if(!uz[a[x][i]]) DFS(a[x][i]);
}

void Parcurgere()
{
    int total = 0;
    for(int i = 1; i <= n; i++)
    if(uz[i] == 0)
    {
        total++;
        DFS(i);
    }
    fout << total;
}

int main()
{
    Citire();
    Parcurgere();
    return 0;
}