Cod sursa(job #2614727)

Utilizator MocalinnoMoca Andrei Catalin Mocalinno Data 12 mai 2020 16:31:32
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>
#define DAU  ios::sync_with_stdio(false); fin.tie(0); fout.tie(0);
#define PLEC fin.close(); fout.close(); return 0;
using namespace std;
const string problem("dfs");
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");
using VI  = vector<int>;
using VVI = vector<VI>;
using VB  = vector<bool>;
VVI g;
VB viz;
inline void DFS(int x) {
    for (const int& y : g[x])
        if (!viz[y])
            viz[y] = true, DFS(y);
}
int n, m, x, y, nrcomp;
int main() {
    DAU
    fin >> n >> m;
    g = VVI(n + 1);
    for (int i = 1; i <= m; ++i) {
        fin >> x >> y;
        g[x].emplace_back(y);
    }
    viz = VB(n + 1);
    for (int i = 1; i <= n; ++i)
        if (!viz[i])
            viz[i] = true, DFS(i), ++nrcomp;
    fout << nrcomp;
    PLEC
}