Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct tree {
  7. int k, l, r, Lmax, Rmin;
  8. };
  9. int i, n;
  10. tree v[200005];
  11.  
  12. bool Correct(int j)
  13. {
  14. if (v[j].k >= v[j].Lmax || v[j].k <= v[j].Rmin) return 0;
  15. if (v[j].l == 0 and v[j].r == 0) return 1;
  16. if (v[j].l == 0)
  17. return Correct(v[j].r);
  18. if (v[j].r == 0)
  19. return Correct(v[j].l);
  20. return (Correct(v[j].l) & Correct(v[j].r));
  21. }
  22.  
  23. int main()
  24. {
  25. ios_base::sync_with_stdio(false);
  26. cin.tie(NULL);
  27. cout.tie(NULL);
  28. freopen("check.in", "r", stdin);
  29. freopen("check.out", "w", stdout);
  30. cin >> n;
  31. if (n == 0)
  32. {
  33. cout << "YES";
  34. return 0;
  35. }
  36. cin >> v[1].k >> v[1].l >> v[1].r;
  37. v[1].Lmax = 1000000000;
  38. v[1].Rmin = -1000000000;
  39. v[v[1].l].Lmax = v[1].k;
  40. v[v[1].l].Rmin = -1000000000;
  41. v[v[1].r].Rmin = v[1].k;
  42. v[v[1].r].Lmax = 1000000000;
  43. for (i = 2; i <= n; i++)
  44. {
  45. cin >> v[i].k >> v[i].l >> v[i].r;
  46. v[v[i].l].Lmax = v[i].k;
  47. v[v[i].l].Rmin = v[i].Rmin;
  48.  
  49. v[v[i].r].Rmin = v[i].k;
  50. v[v[i].r].Lmax = v[i].Lmax;
  51. }
  52.  
  53. if (Correct(1))
  54. cout << "YES";
  55. else
  56. cout << "NO";
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement