Advertisement
DobriyKrot

10B

Dec 25th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4.  
  5. using namespace std;
  6.  
  7. const int INF = 1e9 + 1;
  8. const int MAXN = 100000;
  9.  
  10. vector<int> g[MAXN];
  11. int pr[MAXN];
  12. bool dp[MAXN];
  13.  
  14. void dfs(int ver) {
  15. for (const auto& i : g[ver]) {
  16. if (i != pr[ver]) {
  17. dfs(i);
  18. if (!dp[i]) {
  19. dp[ver] = true;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. int main() {
  26. ios_base::sync_with_stdio(false);
  27. cin.tie(nullptr);
  28. int n;
  29. cin >> n;
  30. int root = 0;
  31. for (int i = 0; i < n; ++i) {
  32. cin >> pr[i];
  33. --pr[i];
  34. if (pr[i] == -1) {
  35. root = i;
  36. } else {
  37. g[pr[i]].emplace_back(i);
  38. }
  39. }
  40. dfs(root);
  41. cout << (dp[root] ? "YES" : "NO");
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement