Cod sursa(job #2685891)

Utilizator tact1m4n3Dicu Tudor Andrei tact1m4n3 Data 17 decembrie 2020 22:16:22
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <bits/stdc++.h>

using namespace std;

const int lim = 100001;
int N, M, i;
vector<int> adj[lim];
bool visited[lim];
int cc = 0;

void dfs(int s)
{
    if (visited[s]) return;
    visited[s] = true;
    cc++;
    for (auto u : adj[s])
        dfs(u);
}

int main()
{
    fstream infile("dfs.in");
    fstream outfile("dfs.out");
    infile >> N >> M;
    for (i = 0; i < M; i++)
    {
        int n, m;
        infile >> n >> m;
        adj[n].push_back(m);
    }

    dfs(1);

    outfile << cc;

    return 0;
}