Advertisement
snowywhitee

Untitled

Dec 20th, 2019
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. struct node {
  8.     int key;
  9.     node *l, *r, *p;
  10.     int h;
  11. };
  12. struct things {
  13.     int key, l, r;
  14.     node *q;
  15.     int lh, rh, h;
  16.     int flag;
  17. };
  18. int balf(vector < things > &arr, int i)
  19. {
  20.     if ( i < 0)
  21.     {
  22.         return -1;
  23.     }
  24.     arr[i].lh = 1 + balf(arr, arr[i].l);
  25.     arr[i].rh = 1 + balf(arr, arr[i].r);
  26.     arr[i].h = arr[i].rh - arr[i].lh;
  27.     arr[i].q->h = max(arr[i].lh, arr[i].rh);
  28.    
  29.     return max(arr[i].lh, arr[i].rh);
  30. }
  31.  
  32. int mheight(node *peak)
  33. {
  34.     if ( peak == NULL)
  35.     {
  36.         return -1;
  37.     }
  38.     return max(1 + mheight(peak->l), 1 + mheight(peak->r));
  39. }
  40.  
  41. void bigleft(node *peak, node *&root)
  42. {
  43.     if (peak->p == NULL || peak == root)//root
  44.     {
  45.         root = peak->r->l;
  46.         peak->r->l->p = NULL;
  47.     } else if (peak->p->l == peak) //wl
  48.     {
  49.         peak->p->l = peak->r->l;
  50.         peak->r->l->p = peak->p;
  51.     } else if (peak->p->r == peak) //wr
  52.     {
  53.         peak->p->r = peak->r->l;
  54.         peak->r->l->p = peak->p;
  55.     }
  56.     node *tmpl = peak->r->l->l;
  57.     node *tmpr = peak->r->l->r;
  58.     peak->r->l->l = peak;
  59.     peak->r->l->r = peak->r;
  60.     peak->p = peak->r->l;
  61.     peak->r->p = peak->r->l;
  62.     peak->p->r->l = tmpr;
  63.     peak->p->l->r = tmpl;
  64.    
  65. }
  66.  
  67. void smallleft(node *peak, node *&root)
  68. {
  69.     if (peak->p == NULL || peak == root) //root
  70.     {
  71.         root = peak->r;
  72.         peak->r->p = NULL;
  73.     } else if (peak->p->l == peak) //wl
  74.     {
  75.         peak->p->l = peak->r;
  76.         peak->r->p = peak->p;
  77.     } else if (peak->p->r == peak) //wr
  78.     {
  79.         peak->p->r = peak->r;
  80.         peak->r->p = peak->p;
  81.     }
  82.     node *tmpl = peak->r->l;
  83.     peak->r->l = peak;
  84.     peak->p = peak->r;
  85.     peak->r = tmpl;
  86.    
  87. }
  88.  
  89. void setf(node *peak, vector < things > &res, int i)
  90. {
  91.     if (peak != NULL)
  92.     {
  93.         if (peak->l != NULL)
  94.         {
  95.             res[2*i + 1].key = peak->l->key;
  96.             res[2*i + 1].flag = 0;
  97.             if (peak->l->l == NULL)
  98.             {
  99.                 res[2*i + 1].l = -1;
  100.             } else {
  101.                 res[2*i + 1].l = 2*(2*i+1)+1;
  102.             }
  103.             if (peak->l->r == NULL)
  104.             {
  105.                 res[2*i + 1].r = -1;
  106.             } else {
  107.                 res[2*i + 1].r = 2*(2*i+1)+2;
  108.             }
  109.            
  110.             setf(peak->l, res, i + 1);
  111.         }
  112.         if (peak->r != NULL)
  113.         {
  114.             res[2*i + 2].key = peak->r->key;
  115.             res[2*i + 2].flag = 0;
  116.             if (peak->r->l == NULL)
  117.             {
  118.                 res[2*i + 2].l = -1;
  119.             } else {
  120.                 res[2*i + 2].l = 2*(2*i+2)+1;
  121.             }
  122.             if (peak->r->r == NULL)
  123.             {
  124.                 res[2*i + 2].r = -1;
  125.             } else {
  126.                 res[2*i + 2].r = 2*(2*i+2)+2;
  127.             }
  128.            
  129.             setf(peak->r, res, i + 2);
  130.         }
  131.     }
  132. }
  133. int arrsize(int loop)
  134. {
  135.     int c = 0;
  136.     int remain = 1;
  137.     if (loop == 0)
  138.     {
  139.         return remain;
  140.     } else {
  141.         while (c != loop)
  142.         {
  143.             c++;
  144.             remain = remain + pow(2, c);
  145.         }
  146.         return remain;
  147.     }
  148. }
  149.  
  150. int main()
  151. {
  152.     ifstream fin("rotation.in");
  153.     ofstream fout("rotation.out");
  154.    
  155.     vector < things > arr;
  156.    
  157.     int n;
  158.     int m;
  159.     fin >> n;
  160.     m = n;
  161.     if (n > 1)
  162.     {
  163.         arr.resize(n);
  164.         int key, num1, num2;
  165.         node *parent = NULL;
  166.         for (int i = 0; i < n; i++)
  167.         {
  168.             fin >> key >> num1 >> num2;
  169.             arr[i].key = key;
  170.             arr[i].l = --num1;
  171.             arr[i].r = --num2;
  172.            
  173.             node *q = NULL;
  174.             q = new node();
  175.             q->key = key;
  176.             q->l = NULL;
  177.             q->r = NULL;
  178.             q->p = parent;
  179.             arr[i].q = q;
  180.             parent = q;
  181.         }
  182.         for (int i = 0; i < n; i++)
  183.         {
  184.             arr[i].q->l = arr[arr[i].l].q;
  185.             arr[i].q->r = arr[arr[i].r].q;
  186.         }
  187.         node *root = arr[0].q;
  188.         for (int i = 0; i < n; i++)
  189.         {
  190.         //  cout << arr[i].q->key<< " "<< arr[i].l<< " "<< arr[i].r<<endl;
  191.         }
  192.     //  cout << "array filled\n";
  193.         balf(arr, 0);
  194.         for (int i = 0; i < n; i++)
  195.         {
  196.             if (arr[i].h > 1)
  197.             {
  198.                 if (arr[arr[i].r].h == -1)
  199.                 {
  200.                     bigleft(root, root);
  201.                 } else {
  202.                     smallleft(root, root);
  203.                 }
  204.                 i = n +1;
  205.             }
  206.         }
  207.         int size = arrsize(mheight(root));
  208.  
  209.         things some;
  210.         some.flag = 1;
  211.         vector < things > res (size, some);
  212.         setf(root, res, 0);
  213.  
  214.         fout << n << endl;
  215.        
  216.         res[0].key = root->key;
  217.         res[0].flag = 0;
  218.         if (root->l == NULL && root->l == NULL)
  219.         {
  220.             res[0].l = -1;
  221.             res[0].l = -1;
  222.         } else {
  223.             res[0].l = 1;
  224.             res[0].r = 2;
  225.             if (root->l == NULL)
  226.             {
  227.                 res[0].l  =  -1;
  228.             }
  229.             if (root->r == NULL)
  230.             {
  231.                 res[0].r  =  -1;
  232.             }
  233.         }
  234.         int j = 3;
  235.         int c = 0;
  236.         for (int i = 0; i < size; i++)
  237.         {
  238.             if (res[i].flag == 0)
  239.             {
  240.                 c++;
  241.                 if (i > 0 && c <= (n/2))
  242.                 {
  243.                     if (res[i].l != -1 && res[i].r != -1)
  244.                     {
  245.                         res[i].l = j;
  246.                         res[i].r = j + 1;
  247.                         j = j + 2;
  248.                     } else if (res[i].l == -1 && res[i].r != -1)
  249.                     {
  250.                         res[i].r = j;
  251.                         j = j + 1;
  252.                     } else if (res[i].l != -1 && res[i].r == -1)
  253.                     {
  254.                         res[i].l = j;
  255.                         j = j + 1;
  256.                     }
  257.                 }
  258.                 fout << res[i].key<<" "<< ++res[i].l<<" " << ++res[i].r <<endl;
  259.             }
  260.         }
  261.     } else {
  262.         if (n == 0)
  263.         {
  264.             fout << 0<<endl;
  265.         } else if (n == 1)
  266.         {
  267.             fout << n<<endl;
  268.             int key;
  269.             fin >> key;
  270.             fout<< key<<" "<<0<<" "<<0<<endl;
  271.         }
  272.     }
  273.    
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement