Advertisement
Guest User

Depth Search

a guest
Apr 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. int n, m, a, b, cnt, arr[101][101], lamps[101], sum;
  10.  
  11. void dfs(int v)
  12. {
  13.     cnt++;
  14.     lamps[v] = 1;
  15.    
  16.     for (int i = 0; i < n; i++)
  17.     {
  18.         if(arr[v][i] && !lamps[i]) dfs(i);
  19.     }
  20. }
  21.  
  22. int main()
  23. {
  24.     //--------------------------------
  25.     ios_base::sync_with_stdio(false);
  26.     cin.tie(0);
  27.     cout.tie(0);
  28.     //--------------------------------
  29.    
  30.     cin >> n >> m;
  31.    
  32.     for (int i = 0; i < n; i++)
  33.     {
  34.         for (int j = 0; j < n; j++)
  35.         {
  36.             cin >> arr[i][j];
  37.         }
  38.     }
  39.    
  40.     dfs(m-1);
  41.    
  42.     cout << cnt;
  43.    
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement