Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<vector>
- using std::vector;
- using std::ifstream;
- using std::ofstream;
- using std::endl;
- bool and_op_k(vector<bool>first, vector<bool>second, long long k) {
- for (int i = 0; i < k + 1; i++) {
- if (second[k - i] && first[i]) {
- return 1;
- }
- }
- return 0;
- }
- vector<bool> and_op(vector<bool>first, vector<bool>second) {
- vector<bool> res(first.size());
- int i=0;
- for (auto it=first.begin();it<first.end();it++, i++) {
- res[i] = *it || second[i];
- }
- return res;
- }
- vector<bool> move(vector<bool> vec) {
- vector<bool> moved(1,false);
- int i=0;
- for (auto it=vec.begin();it<vec.end();it++,i++) {
- moved.push_back(vec.at(i));
- }
- if (vec[vec.size()-1] ){
- moved.pop_back();
- moved.push_back(true);
- }
- return moved;
- }
- struct Node {
- long long value;
- Node* son_l;
- Node* son_r;
- vector<bool> path_l;
- vector<bool> path_r;
- vector<bool> path_u;
- Node() {
- Node(100, false);
- }
- Node(long long k, bool tr) {
- path_r = vector<bool>(k + 1,0);
- path_u = vector<bool>(k + 1,0);
- path_l = vector<bool>(k + 1,0);
- if(tr){
- son_l = NULL;
- son_r = NULL;
- }
- }
- bool canRem(long long k) {
- if (and_op_k(path_l, path_r, k)){
- return false;
- } else if(and_op_k(path_l, path_u, k)){
- return false;
- } else if(and_op_k(path_r, path_u, k)){
- return false;
- } else if(path_l[k]){
- return false;
- } else if(path_r[k]){
- return false;
- } else if(path_u[k]){
- return false;
- } else{
- return true;
- }
- }
- };
- class BinaryTree {
- public:
- Node* root;
- BinaryTree() {
- root = NULL;
- }
- BinaryTree(Node*given_root) {
- root = given_root;
- }
- void insrt(long long key, long long k) {
- if (root == NULL) {
- root = new Node(k,true);
- root->value = key;
- }
- else {
- Node * next = root;
- Node * previous = NULL;
- long long val1,val2;
- while (next!=NULL) {
- previous = next;
- val1=next->value;
- if (key < val1) {
- next = next->son_l;
- } else if (key > val1) {
- next = next->son_r;
- }
- else if (key == val1) {
- break;
- }
- }
- val2=previous->value;
- if (key < val2) {
- previous->son_l = new Node(k,true);
- previous->son_l->value = key;
- }
- if (key > val2) {
- previous->son_r = new Node(k,true);
- previous->son_r->value = key;
- }
- }
- }
- void rm(Node*node) {
- if (node != NULL) {
- if (node->son_r == NULL) {
- if (node->son_l == NULL) {
- node->value = -1;
- }
- else {
- node->value = node->son_l->value;
- node->son_r = node->son_l->son_r;
- node->son_l = node->son_l->son_l;
- }
- }
- else {
- Node*node_to_remove = node->son_r;
- while (node_to_remove->son_l != NULL)
- {
- node_to_remove = node_to_remove->son_l;
- }
- node->value = node_to_remove->value;
- rm(node_to_remove);
- }
- }
- }
- };
- void postorder(Node*root, Node*new_root,long long k) {
- if (root->son_l != NULL) {
- postorder(root->son_l, new_root, k);
- root->path_l = move(and_op(root->son_l->path_l, root->son_l->path_r));
- }
- if (root->son_r != NULL) {
- postorder(root->son_r, new_root, k);
- root->path_r = move(and_op(root->son_r->path_l, root->son_r->path_r));
- }
- if (root->son_l == NULL && root->son_r == NULL) {
- root->path_l[0] = true;
- root->path_r[0] = true;
- }
- }
- Node* preorder(Node*root, long long k) {
- if (root->son_r != NULL) {
- root->son_r->path_u = move(and_op(root->path_u, root->path_l));
- Node*right_max = preorder(root->son_r, k);
- if (right_max != NULL){
- return right_max;
- }
- }
- if (root->canRem(k)) {
- return root;
- }
- else {
- if (root->son_l != NULL) {
- root->son_l->path_u = move(and_op(root->path_u, root->path_r));
- return preorder(root->son_l, k);
- }
- return NULL;
- }
- }
- void write(Node*root, ofstream& out) {
- if (root != NULL && root->value!=-1) {
- out << root->value << endl;
- write(root->son_l, out);
- write(root->son_r, out);
- }
- }
- void handle1(BinaryTree& tree,long long k){
- Node* new_root = tree.root;
- postorder(tree.root, new_root, k);
- if (tree.root->son_l == NULL) {
- tree.root->path_l[0] = true;
- }
- if (tree.root->son_r == NULL) {
- tree.root->path_r[0] = true;
- }
- Node* max = preorder(tree.root, k);
- tree.rm(max);
- }
- void handle2(BinaryTree& tree,long long k){
- Node*next = tree.root;
- while (next->son_r != NULL) {
- next = next->son_r;
- }
- tree.rm(next);
- }
- int main() {
- ifstream in("tst.in");
- ofstream out("tst.out");
- BinaryTree tree;
- long long k,val;
- in>>k;
- //
- int num = 0;
- while (!in.eof()) {
- in>>val;
- tree.insrt(val, k);
- num++;
- }
- if (( num == 0 || num == 1 ) && k!=0) {
- out << "Empty" << endl;
- }
- if (k < 1000) {
- if (num == 1 && k == 0) {
- out << tree.root->value << endl;
- }
- else {
- handle1(tree,k);
- write(tree.root, out);
- }
- } else{
- if (num == 1) {
- out << "Empty" << endl;
- } else if(num == 0){
- out << "Empty" << endl;
- }
- else {
- handle2(tree,k);
- write(tree.root, out);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment