Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- typedef long long ll;
- struct Node {
- ll value;
- int left;
- int right;
- Node() { }
- void set(ll value, int left, int right) {
- Node::value = value;
- Node::left = !left ? 0 : left - 1;
- Node::right = !right ? 0 : right - 1;
- }
- };
- bool check(vector<Node>& table, size_t i) {
- auto cur = &table[i];
- //leaf -> ok
- if (!cur->left)
- if (!cur->right)
- return true;
- // *left, 0
- // compare values, then check left son
- if (!cur->right)
- if (cur->left)
- return table[cur->left].value >= cur->value ? false : check(table, cur->left);
- // 0, *right
- // compare values, then check right son
- if (!cur->left)
- if (cur->right)
- return table[cur->right].value <= cur->value ? false : check(table, cur->right);
- // *left, *right
- // compare values, then check both
- if (table[cur->left].value >= cur->value)
- return false;
- if (table[cur->right].value <= cur->value)
- return false;
- if (!check(table, cur->left))
- return false;
- if (!check(table, cur->right))
- return false;
- // if all pass -> allright
- return true;
- }
- int main()
- {
- ifstream ist("check.in");
- ofstream ost("check.out");
- size_t n;
- ist >> n;
- vector<Node>* table = new vector<Node>(n);
- for (size_t i = 0; i < n; i++) {
- ll value;
- int left, right;
- ist >> value >> left >> right;
- table->at(i).set(value, left, right);
- }
- ost << (n == 0 ? "YES" : (check(*table, 0) ? "YES" : "NO"));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement