Cod sursa(job #2376639)

Utilizator FlorinVladutCreta Florin FlorinVladut Data 8 martie 2019 16:54:28
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

int n, m, k = 0;

vector<vector<int>> G;
vector<bool> v;


void read()
{
    fin >> n >> m;

    G = vector<vector<int>>(n + 1);
    v = vector<bool>(n + 1);

    int a, b;

    while(fin >> a >> b)
    {
        G[a].push_back(b);
    }
}

void DFS(int x)
{
    v[x] = true;

    for(auto& i : G[x])
    {
        if(!v[i])
        {
            v[i] = true;
            DFS(i);
        }
    }

}

void write()
{
    fout << k;
}

int main()
{
    read();

    for(int i = 1; i <= n; i++)
    {
        if(!v[i])
        {
            DFS(i);
            k++;
        }
    }

    write();

    return 0;
}