Cod sursa(job #2211410)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 10 iunie 2018 11:23:47
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>

using namespace std;

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

vector <int> G[100001];

int n, m, x, y, nr;

bool viz[100001];

void dfs (int nod)
{
    viz[nod] = true;

    for (int i = 0; i < G[nod].size(); i++)
    {
        int x = G[nod][i];
        if (viz[x] == false) dfs(x);
    }
}
int main()
{
    f >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

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

    g << nr << '\n';
    return 0;
}