Cod sursa(job #2132263)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 15 februarie 2018 17:06:02
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");

int n, nrc;
vector <int> L[100007];
bitset <100007> viz;

void DFS(int nod)
{
    viz[nod] = 1;
    for (auto i : L[nod])
        if (viz[i] == 0)
            DFS(i);
}

int main()
{
    int i, k, x, y;
    fin >> n >> k;
    for (i = 1; i <= k; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
        {
            nrc++;
            DFS(i);
        }
    fout << nrc << "\n";
    fin.close();
    fout.close();
    return 0;
}