Cod sursa(job #2564985)

Utilizator NotTheBatmanBruce Wayne NotTheBatman Data 2 martie 2020 11:28:47
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

const int N = 1e5 + 5, oo = 1e9;

int n;
vector <int> L[N];

void Read ()
{
    int m;
    ifstream fin ("dfs.in");
    fin >> n >> m;
    while (m--)
    {
        int x, y;
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
}

bitset <N> seen;

void DFS (int vertex)
{
    seen[vertex] = 1;
    for (auto next : L[vertex])
        if (!seen[next])
            DFS(next);
}

void Solve ()
{
    int cc = 0;
    for (int i = 1; i <= n; i++)
        if (!seen[i])
        {
            cc++;
            DFS(i);
        }
    ofstream fout ("dfs.out");
    fout << cc << "\n";
    fout << "\n";
    fout.close();
}

int main()
{
    Read();
    Solve();
    return 0;
}