Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- struct tree {
- int k, l, r, Lmax, Rmin;
- };
- int i, n;
- tree v[200005];
- bool Correct(int j)
- {
- if (v[j].k >= v[j].Lmax || v[j].k <= v[j].Rmin) return 0;
- if (v[j].l == 0 and v[j].r == 0) return 1;
- if (v[j].l == 0)
- return Correct(v[j].r);
- if (v[j].r == 0)
- return Correct(v[j].l);
- return (Correct(v[j].l) & Correct(v[j].r));
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- freopen("check.in", "r", stdin);
- freopen("check.out", "w", stdout);
- cin >> n;
- if (n == 0)
- {
- cout << "YES";
- return 0;
- }
- cin >> v[1].k >> v[1].l >> v[1].r;
- v[1].Lmax = 1000000000;
- v[1].Rmin = -1000000000;
- v[v[1].l].Lmax = v[1].k;
- v[v[1].l].Rmin = -1000000000;
- v[v[1].r].Rmin = v[1].k;
- v[v[1].r].Lmax = 1000000000;
- for (i = 2; i <= n; i++)
- {
- cin >> v[i].k >> v[i].l >> v[i].r;
- v[v[i].l].Lmax = v[i].k;
- v[v[i].l].Rmin = v[i].Rmin;
- v[v[i].r].Rmin = v[i].k;
- v[v[i].r].Lmax = v[i].Lmax;
- }
- if (Correct(1))
- cout << "YES";
- else
- cout << "NO";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement