Cod sursa(job #2203861)

Utilizator oso.andinoooIonut Stan oso.andinooo Data 13 mai 2018 15:23:55
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>
using namespace std;

const int N = 100001;
vector <int> g[N];
int f[N];

int lgput(int n) {
    f[n] = 1;
    for (auto v : g[n]) {
        if (f[v] == 0)
            lgput(v); } }

int main() {
    freopen("dfs.in", "r", stdin);
    freopen("dfs.out", "w", stdout);
    int nr = 0, n, m, x, y;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; i++) {
       scanf("%d %d", &x, &y);
       g[x].push_back(y);
       g[y].push_back(x); }
    for (int i = 1; i <= n; i++) {
        if (f[i] == 0) {
            nr++;
            lgput(i); } }
    printf("%d\n", nr);
    return 0; }