fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. void dfs(int cur, vector<vector<int>> & adj, vector<bool> & was, int & count){
  9. if(!was[cur]){
  10. was[cur] = true;
  11. count++;
  12. for(int i = 0; i < adj[cur].size(); i++){
  13. dfs(adj[cur][i], adj, was, count);
  14. }
  15. }
  16. }
  17.  
  18. int getCount(int start, vector<vector<int>> & adj){
  19. vector<bool> was(adj.size(), false);
  20. int count = 0;
  21. dfs(start, adj, was, count);
  22. return count;
  23. }
  24.  
  25. int main() {
  26. int n;
  27. cin >> n;
  28. int start;
  29. cin >> start;
  30. start--;
  31. vector<vector<int>> adj(n, vector<int>());
  32. for(int i = 0; i < n; i++){
  33. for(int j = 0; j < n; j++){
  34. int temp;
  35. cin >> temp;
  36. if(temp == 1){
  37. adj[i].push_back(j);
  38. }
  39. }
  40. }
  41. cout << getCount(start, adj);
  42. return 0;
  43. }
Success #stdin #stdout 0s 4528KB
stdin
5 3
0 1 0 0 1
1 0 1 0 1
0 1 0 1 0
0 0 1 0 1
1 1 0 1 0
stdout
5