Submission #766960

# Submission time Handle Problem Language Result Execution time Memory
766960 2023-06-26T09:37:36 Z simene Mechanical Doll (IOI18_doll) C++14
Compilation error
0 ms 0 KB
#include "doll.h"
#include <vector>

class Node 
{
public:
    Node* left;
    Node* right;
    Node* parent;
    bool state = false;
    int ltrig = -1;
    int rtrig = -1;
    int id = -1;

    void construct(int lvls) 
    {
        if (lvls <= 0) return;

        left = new Node();
        right = new Node();

        left->parent = this;
        right->parent = this;

        left->construct(lvls - 1);
        right->construct(lvls - 1);
    }

    Node* traverse() 
    {
        if (left == nullptr) return this;

        if (state) 
        {
            state = !state;
            return right;
        }
        else 
        {
            state = !state;
            return left;
        }
    }

    void insert(int trig) 
    {
        Node* ins = traverse();

        ins->state = !ins->state;

        if (ins->state) ins->ltrig = trig;
        else ins->right = trig;
    }

    int prune() 
    {
        int s = 1;

        if (right != nullptr) s += right->prune;
        if (left != nullptr) s += left->prune;

        if (right == nullptr && rtrig == -1) 
        {
            if (left != nullptr) 
            {
                if (parent->left == this) parent->left = left;
                else parent->right = left;
            }
            else if (ltrig != -1) 
            {
                if (parent->left == this) parent->ltrig = ltrig;
                else parent->rtrig = ltrig;
            }
            else 
            {
                if (parent->left == this) parent->left = nullptr;
                else parent->left = nullptr;

                return 0;
            }
        }

        return s;
    }

    int identify(int nextid, int* X[], int* Y[])
    {
        id = nextid--;

        if (left != nullptr) nextid = left->identify(nextid, X, Y);
        if (right != nullptr) nextid = right->identify(nextid, X, Y);

        if (left != nullptr) X[-j - 1] = left->id;
        else X[-j - 1] = ltrig;

        if (right != nullptr) Y[-j - 1] = right->id;
        else Y[-j - 1] = rtrig;

        return nextid;
    }
};

void create_circuit(int M, std::vector<int> A) 
{
    Node* root;

    A.push_back(0);

    int N = A.size();

    int height = (int)ceil(log2(N));

    root->construct(height);

    for (int i : A) 
    {
        root->insert(i);
    }

    int s = root->prune;

    int C[M + 1];
    for (int i = 0; i < M + 1; i++) C[i] = -1;

    int X[s];
    int Y[s];

    root->identify(-1, &X, &Y);

    answer(C, X, Y);
}

Compilation message

doll.cpp: In member function 'void Node::insert(int)':
doll.cpp:52:27: error: invalid conversion from 'int' to 'Node*' [-fpermissive]
   52 |         else ins->right = trig;
      |                           ^~~~
      |                           |
      |                           int
doll.cpp: In member function 'int Node::prune()':
doll.cpp:59:43: error: invalid use of member function 'int Node::prune()' (did you forget the '()' ?)
   59 |         if (right != nullptr) s += right->prune;
      |                                    ~~~~~~~^~~~~
      |                                                ()
doll.cpp:60:41: error: invalid use of member function 'int Node::prune()' (did you forget the '()' ?)
   60 |         if (left != nullptr) s += left->prune;
      |                                   ~~~~~~^~~~~
      |                                              ()
doll.cpp: In member function 'int Node::identify(int, int**, int**)':
doll.cpp:93:33: error: 'j' was not declared in this scope
   93 |         if (left != nullptr) X[-j - 1] = left->id;
      |                                 ^
doll.cpp:94:17: error: 'j' was not declared in this scope
   94 |         else X[-j - 1] = ltrig;
      |                 ^
doll.cpp:96:34: error: 'j' was not declared in this scope
   96 |         if (right != nullptr) Y[-j - 1] = right->id;
      |                                  ^
doll.cpp:97:17: error: 'j' was not declared in this scope
   97 |         else Y[-j - 1] = rtrig;
      |                 ^
doll.cpp: In function 'void create_circuit(int, std::vector<int>)':
doll.cpp:111:28: error: 'log2' was not declared in this scope
  111 |     int height = (int)ceil(log2(N));
      |                            ^~~~
doll.cpp:111:23: error: 'ceil' was not declared in this scope
  111 |     int height = (int)ceil(log2(N));
      |                       ^~~~
doll.cpp:120:19: error: cannot convert 'Node::prune' from type 'int (Node::)()' to type 'int'
  120 |     int s = root->prune;
      |                   ^~~~~
doll.cpp:128:24: error: cannot convert 'int (*)[s]' to 'int**'
  128 |     root->identify(-1, &X, &Y);
      |                        ^~
      |                        |
      |                        int (*)[s]
doll.cpp:86:35: note:   initializing argument 2 of 'int Node::identify(int, int**, int**)'
   86 |     int identify(int nextid, int* X[], int* Y[])
      |                              ~~~~~^~~
doll.cpp:130:12: error: could not convert '(int*)(& C)' from 'int*' to 'std::vector<int>'
  130 |     answer(C, X, Y);
      |            ^
      |            |
      |            int*