fork(13) download
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int n;
  8. int s;
  9. cin>>n>>s;
  10. s--;
  11. int matrix[n][n];
  12. stack<int> st;
  13. int counter=1;
  14. for(int i=0;i<n;i++)
  15. {
  16. for(int j=0;j<n;j++)
  17. {
  18. cin>>matrix[i][j];
  19. }
  20. }
  21. for(int j=0;j<n;j++)
  22. {
  23. if(matrix[s][j] == 1) st.push(j);
  24. }
  25. matrix[s][s]=1;
  26. while(!st.empty())
  27. {
  28. int a=st.top();
  29. st.pop();
  30. if(matrix[a][a]!=1)
  31. {
  32. for(int j=0;j<n;j++)
  33. {
  34. if(matrix[a][j] == 1) st.push(j);
  35. }
  36. counter++;
  37. matrix[a][a]=1;
  38. }
  39.  
  40. }
  41. cout<<counter<<endl;
  42. }
Success #stdin #stdout 0s 3280KB
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