Pagini recente » Cod sursa (job #2138239) | Cod sursa (job #84763) | Cod sursa (job #488061) | Cod sursa (job #1072078) | Cod sursa (job #3156653)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
void DFS(int x, vector <int> G[], int vis[]) {
vis[x] = 1;
for(auto next : G[x]) {
if(vis[next] == 0) DFS(next, G, vis);
}
}
int main() {
const int NMAX = 100000;
int N, M, nr_componente_conexe = 0, vis[NMAX + 1] = {0};
f >> N >> M;
vector <int> G[N + 1];
for(int i = 1; i <= M; i++) {
int x, y;
f >> x >> y;
G[x].push_back(y);
}
for(int j = 1; j <= N; j++) {
if(vis[j] == 0) {
nr_componente_conexe++;
DFS(j, G, vis);
}
}
g << nr_componente_conexe;
}