Advertisement
snowywhitee

Untitled

Dec 7th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. vector < vector < int > > arr;
  7.  
  8. bool dfs(vector < vector < int > > &arr, int pos, int left, int right)
  9. {
  10.     if (pos < 0)
  11.     {
  12.         return true;
  13.     }
  14.     if (arr[pos][0] >= right || arr[pos][0] <= left)
  15.     {
  16.         return false;
  17.     }
  18.      
  19.     return (dfs(arr, arr[pos][1], left, arr[pos][0]) && dfs(arr, arr[pos][2], arr[pos][0], right));
  20. }
  21.  
  22. int main()
  23. {
  24.     ifstream fin("check.in");
  25.     ofstream fout("check.out");
  26.      
  27.     int n;
  28.     fin >> n;
  29.     arr.resize(n);
  30.      
  31.     int key, num1, num2;
  32.     if (n == 0)
  33.     {
  34.         fout << "YES";
  35.     } else {
  36.         for (int i = 0; i < n; i++)
  37.         {
  38.             fin >> key >> num1 >> num2;
  39.             arr[i].push_back(key);
  40.             arr[i].push_back(num1 - 1);
  41.             arr[i].push_back(num2 - 1);
  42.         }
  43.          
  44.         if (dfs(arr, 0, INT32_MIN, INT32_MAX))
  45.         {
  46.             fout << "YES";
  47.         } else {
  48.             fout << "NO";
  49.         }
  50.     }
  51.      
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement