YauhenMardan

Untitled

May 28th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4. using std::vector;
  5. using std::ifstream;
  6. using std::ofstream;
  7. using std::endl;
  8.  
  9. bool and_op_k(vector<bool>first, vector<bool>second, long long k) {
  10. for (int i = 0; i < k + 1; i++) {
  11. if (second[k - i] && first[i]) {
  12. return 1;
  13. }
  14. }
  15. return 0;
  16. }
  17.  
  18. vector<bool> and_op(vector<bool>first, vector<bool>second) {
  19. vector<bool> res(first.size());
  20. int i=0;
  21. for (auto it=first.begin();it<first.end();it++, i++) {
  22. res[i] = *it || second[i];
  23. }
  24. return res;
  25. }
  26.  
  27. vector<bool> move(vector<bool> vec) {
  28. vector<bool> moved(1,false);
  29. int i=0;
  30. for (auto it=vec.begin();it<vec.end();it++,i++) {
  31. moved.push_back(vec.at(i));
  32. }
  33. if (vec[vec.size()-1] ){
  34. moved.pop_back();
  35. moved.push_back(true);
  36. }
  37. return moved;
  38. }
  39.  
  40. struct Node {
  41. long long value;
  42. Node* son_l;
  43. Node* son_r;
  44. vector<bool> path_l;
  45. vector<bool> path_r;
  46. vector<bool> path_u;
  47.  
  48. Node() {
  49. Node(100, false);
  50. }
  51.  
  52. Node(long long k, bool tr) {
  53. path_r = vector<bool>(k + 1,0);
  54. path_u = vector<bool>(k + 1,0);
  55. path_l = vector<bool>(k + 1,0);
  56. if(tr){
  57. son_l = NULL;
  58. son_r = NULL;
  59. }
  60. }
  61.  
  62. bool canRem(long long k) {
  63. if (and_op_k(path_l, path_r, k)){
  64. return false;
  65. } else if(and_op_k(path_l, path_u, k)){
  66. return false;
  67. } else if(and_op_k(path_r, path_u, k)){
  68. return false;
  69. } else if(path_l[k]){
  70. return false;
  71. } else if(path_r[k]){
  72. return false;
  73. } else if(path_u[k]){
  74. return false;
  75. } else{
  76. return true;
  77. }
  78. }
  79. };
  80.  
  81. class BinaryTree {
  82. public:
  83. Node* root;
  84. BinaryTree() {
  85. root = NULL;
  86. }
  87.  
  88. BinaryTree(Node*given_root) {
  89. root = given_root;
  90. }
  91.  
  92. void insrt(long long key, long long k) {
  93. if (root == NULL) {
  94. root = new Node(k,true);
  95. root->value = key;
  96. }
  97. else {
  98. Node * next = root;
  99. Node * previous = NULL;
  100. long long val1,val2;
  101. while (next!=NULL) {
  102. previous = next;
  103. val1=next->value;
  104. if (key < val1) {
  105. next = next->son_l;
  106. } else if (key > val1) {
  107. next = next->son_r;
  108. }
  109. else if (key == val1) {
  110. break;
  111. }
  112. }
  113. val2=previous->value;
  114. if (key < val2) {
  115. previous->son_l = new Node(k,true);
  116. previous->son_l->value = key;
  117. }
  118. if (key > val2) {
  119. previous->son_r = new Node(k,true);
  120. previous->son_r->value = key;
  121. }
  122. }
  123.  
  124. }
  125.  
  126.  
  127. void rm(Node*node) {
  128. if (node != NULL) {
  129. if (node->son_r == NULL) {
  130. if (node->son_l == NULL) {
  131. node->value = -1;
  132. }
  133. else {
  134. node->value = node->son_l->value;
  135. node->son_r = node->son_l->son_r;
  136. node->son_l = node->son_l->son_l;
  137. }
  138. }
  139. else {
  140. Node*node_to_remove = node->son_r;
  141. while (node_to_remove->son_l != NULL)
  142. {
  143. node_to_remove = node_to_remove->son_l;
  144. }
  145. node->value = node_to_remove->value;
  146. rm(node_to_remove);
  147. }
  148. }
  149.  
  150. }
  151. };
  152.  
  153. void postorder(Node*root, Node*new_root,long long k) {
  154. if (root->son_l != NULL) {
  155. postorder(root->son_l, new_root, k);
  156. root->path_l = move(and_op(root->son_l->path_l, root->son_l->path_r));
  157. }
  158. if (root->son_r != NULL) {
  159. postorder(root->son_r, new_root, k);
  160.  
  161. root->path_r = move(and_op(root->son_r->path_l, root->son_r->path_r));
  162. }
  163. if (root->son_l == NULL && root->son_r == NULL) {
  164. root->path_l[0] = true;
  165. root->path_r[0] = true;
  166. }
  167. }
  168.  
  169. Node* preorder(Node*root, long long k) {
  170. if (root->son_r != NULL) {
  171. root->son_r->path_u = move(and_op(root->path_u, root->path_l));
  172. Node*right_max = preorder(root->son_r, k);
  173. if (right_max != NULL){
  174. return right_max;
  175. }
  176. }
  177.  
  178. if (root->canRem(k)) {
  179. return root;
  180. }
  181. else {
  182. if (root->son_l != NULL) {
  183. root->son_l->path_u = move(and_op(root->path_u, root->path_r));
  184. return preorder(root->son_l, k);
  185. }
  186. return NULL;
  187. }
  188.  
  189. }
  190.  
  191. void write(Node*root, ofstream& out) {
  192. if (root != NULL && root->value!=-1) {
  193. out << root->value << endl;
  194. write(root->son_l, out);
  195. write(root->son_r, out);
  196. }
  197. }
  198.  
  199. void handle1(BinaryTree& tree,long long k){
  200. Node* new_root = tree.root;
  201. postorder(tree.root, new_root, k);
  202. if (tree.root->son_l == NULL) {
  203. tree.root->path_l[0] = true;
  204. }
  205. if (tree.root->son_r == NULL) {
  206. tree.root->path_r[0] = true;
  207. }
  208. Node* max = preorder(tree.root, k);
  209. tree.rm(max);
  210. }
  211.  
  212. void handle2(BinaryTree& tree,long long k){
  213. Node*next = tree.root;
  214. while (next->son_r != NULL) {
  215. next = next->son_r;
  216. }
  217. tree.rm(next);
  218. }
  219.  
  220.  
  221.  
  222. int main() {
  223. ifstream in("tst.in");
  224. ofstream out("tst.out");
  225. BinaryTree tree;
  226. long long k,val;
  227. in>>k;
  228. //
  229. int num = 0;
  230. while (!in.eof()) {
  231. in>>val;
  232. tree.insrt(val, k);
  233. num++;
  234. }
  235.  
  236. if (( num == 0 || num == 1 ) && k!=0) {
  237. out << "Empty" << endl;
  238. }
  239. if (k < 1000) {
  240. if (num == 1 && k == 0) {
  241. out << tree.root->value << endl;
  242. }
  243. else {
  244. handle1(tree,k);
  245. write(tree.root, out);
  246. }
  247. } else{
  248. if (num == 1) {
  249. out << "Empty" << endl;
  250. } else if(num == 0){
  251. out << "Empty" << endl;
  252. }
  253. else {
  254. handle2(tree,k);
  255. write(tree.root, out);
  256. }
  257. }
  258.  
  259. return 0;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment