fork download
  1. //
  2. // Created by Emin Alizade on 03.01.22.
  3. // https://g...content-available-to-author-only...b.com/emin-alizada/ADA-Design-and-Analysis-of-Algorithm-e-olymp/blob/master/4000%20-%20Depth%20Search/main.cpp
  4. //
  5.  
  6. #include <iostream>
  7. #define MAX 101
  8.  
  9. using namespace std;
  10.  
  11. int n, g[MAX][MAX], used[MAX], s, counter = 0;
  12.  
  13. void dfsMatrix(int v) {
  14. used[v] = 1;
  15. counter++;
  16.  
  17. for (int i = 1; i <= n; ++i) {
  18. if (used[i] == 0 && g[v][i] == 1) {
  19. dfsMatrix(i);
  20. }
  21. }
  22. }
  23.  
  24. int main() {
  25. cin>>n >> s;
  26.  
  27. for (int i = 1; i <= n; ++i) {
  28. for (int j = 1; j <= n; ++j) {
  29. cin >> g[i][j];
  30. }
  31. }
  32.  
  33. dfsMatrix(s);
  34.  
  35. cout << counter;
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5504KB
stdin
5 1
0 1 1 0 0
1 0 1 0 0
1 1 0 0 0
0 0 0 0 1
0 0 0 1 0
stdout
3