Cod sursa(job #1805915)

Utilizator florin.elfusFlorin Elfus florin.elfus Data 14 noiembrie 2016 17:43:34
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

vector <int> a[100001];
int f[100001];

int functie(int x){
    f[x] = 1;
    for (int i = 0; i < a[x].size(); i++)
        if (f[a[x][i]] == 0)
            functie(a[x][i]);
}

int main() {
    int n, m ,i;
    ifstream fin ("dfs.in");
    ofstream fout ("dfs.out");
    fin >> n >> m;
    for (i = 1; i <= m; i++){
        int x, y;
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    int rasp = 0;
    for (i = 1; i <= n; i++)
        if (f[i] == 0){
            functie(i);
            rasp ++;
        }
    fout << rasp;
    return 0;
}