Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <stack>
- #include <algorithm>
- using namespace std;
- int main()
- {
- //std::ifstream in("input.txt");
- //std::cin.rdbuf(in.rdbuf());
- //std::ofstream out("output.txt");
- //std::cout.rdbuf(out.rdbuf());
- int n;
- cin >> n;
- vector< vector<int> > adjacentMatrix(n, vector<int>(n));
- int edges = 0;
- int edge;
- for(int i = 0; i < n; ++i)
- {
- for(int j = 0; j < n; ++j)
- {
- cin >> edge;
- edges += adjacentMatrix[i][j] = edge;
- }
- }
- edges /= 2;
- stack<int> stack;
- vector<bool> isVisited(n);
- int visitedCount = 1;
- int u;
- stack.push(0);
- while(!stack.empty())
- {
- u = stack.top();
- stack.pop();
- for(int v = 0; v < n; ++v)
- if(adjacentMatrix[u][v] && !isVisited[v])
- {
- stack.push(v);
- isVisited[u] = true;
- ++visitedCount;
- }
- }
- if(edges == (n - 1) && visitedCount == n)
- cout << "Yes\n";
- else
- cout << "No\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement