Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <stack>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. //std::ifstream in("input.txt");
  12. //std::cin.rdbuf(in.rdbuf());
  13.  
  14. //std::ofstream out("output.txt");
  15. //std::cout.rdbuf(out.rdbuf());
  16.  
  17. int n;
  18. cin >> n;
  19.  
  20. vector< vector<int> > adjacentMatrix(n, vector<int>(n));
  21. int edges = 0;
  22. int edge;
  23.  
  24. for(int i = 0; i < n; ++i)
  25. {
  26. for(int j = 0; j < n; ++j)
  27. {
  28. cin >> edge;
  29. edges += adjacentMatrix[i][j] = edge;
  30. }
  31. }
  32. edges /= 2;
  33.  
  34. stack<int> stack;
  35. vector<bool> isVisited(n);
  36. int visitedCount = 1;
  37. int u;
  38.  
  39. stack.push(0);
  40. while(!stack.empty())
  41. {
  42. u = stack.top();
  43. stack.pop();
  44.  
  45. for(int v = 0; v < n; ++v)
  46. if(adjacentMatrix[u][v] && !isVisited[v])
  47. {
  48. stack.push(v);
  49. isVisited[u] = true;
  50. ++visitedCount;
  51. }
  52. }
  53.  
  54. if(edges == (n - 1) && visitedCount == n)
  55. cout << "Yes\n";
  56. else
  57. cout << "No\n";
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement