Cod sursa(job #2422679)

Utilizator Mickai69Mihai Voica Mickai69 Data 19 mai 2019 16:37:58
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;

void DFS(int node, vector<vector<int>> adj, vector<bool> &viz)
{
    viz[node] = 1;
    for (auto v : adj[node])
        if (viz[v] == 0)
            DFS(v, adj, viz);
}

int main()
{
	ifstream fin ("bfs.in");
	ofstream fout ("bfs.out");
    int n, m, a, b, i, nr = 0;
    fin >> n >> m;
    vector<vector<int>> adj(n + 1);
    for (i = 0; i < m; i++)
    {
        fin >> a >> b;
        adj[a].push_back(b);
    }

    vector<bool> viz(100);

    for (i = 1; i <= n; i++)
    if (viz[i] == 0)
    {
        DFS(i, adj, viz);
        nr++;
    }
    fout << nr;

    return 0;
}