Cod sursa(job #1350961)

Utilizator tudoras8tudoras8 tudoras8 Data 21 februarie 2015 01:19:09
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>
#include <iostream>
#include <vector>

#define MAXN 100001

using namespace std;

int N, M;
vector<int> G[MAXN];
int viz[MAXN], c;

void dfs(int node) {
    viz[node] = 1;
    for (auto a : G[node]) {
        if (!viz[a])
            dfs(a);
    }
}

int main()
{
    ifstream f("dfs.in");
    ofstream g("dfs.out");

    f >> N >> M;

    for (int i = 0; i < M; i++) {
        int x, y;
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    c = 0;
    for (int i = 1; i <= N; i++) {
        if (!viz[i]) {
            dfs(i);
            c++;
        }
    }
    g << c;

    f.close();
    g.close();

    return 0;
}