Cod sursa(job #640293)

Utilizator toniobFMI - Barbalau Antonio toniob Data 25 noiembrie 2011 10:25:35
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>
#include <vector>
using namespace std;

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

int n, m, nr;
vector <int> a[200003];
bool marcat[100002];

void citire () {
    int x, y;
    in >> n >> m;
    while (m--) {
        in >> x >> y;
        a[x].push_back (y);
        a[y].push_back (x);
    }
}

void dfs (int x) {
    marcat[x] = true;
    for (size_t i = 0; i < a[x].size(); ++i) {
        if(!marcat[a[x][i]]) dfs(a[x][i]);
    }
}

int main () {
    citire ();

    for (int i = 1; i <= n; ++i) {
        if (!marcat[i]) {
            ++nr;
            dfs (i);
        }
    }

    out << nr << '\n';
}