Advertisement
wendy890711

朋友圈

Dec 13th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5. void visit(vector<vector<int>>&, vector<bool>&, int);
  6. int findCircleNum(vector<vector<int>>&);
  7.  
  8. int findCircleNum(vector<vector<int>>& M)
  9. {
  10. int ans = 0;
  11. vector<bool> is_visit(M.size(), 0);
  12. //vector<vector<int>> list;
  13. for (int i = 0; i < M.size(); i++)
  14. {
  15. if (is_visit[i] == false)
  16. {
  17. visit(M, is_visit, i);
  18. ans++;
  19. }
  20. }
  21. return ans;
  22. }
  23.  
  24. void visit(vector<vector<int>>& M,vector<bool>& is_visit, int v)
  25. {
  26. is_visit[v] = 1;
  27. for (int i = 0; i < M.size(); i++)
  28. {
  29. if (M[v][i] == 1 && is_visit[i] == 0)
  30. {
  31. visit(M, is_visit, i);
  32. }
  33. }
  34. }
  35.  
  36. int main()
  37. {
  38. vector < vector < int >> in1 =
  39. { { 1, 1, 0 },
  40. { 1, 1, 0 },
  41. { 0, 0, 1 } };
  42.  
  43. vector < vector < int >> in2 =
  44. { { 1, 1, 0 },
  45. { 1, 1, 1 },
  46. { 0, 1, 1 } };
  47.  
  48. cout << "ans is:" << findCircleNum(in1) << endl;
  49. cout << "ans is:" << findCircleNum(in2) << endl;
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement