Problem p02279 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

constexpr int nullNum = -3;

class Node {
public:
  Node(int id_, Node *parent_, std::vector<int> children_)
      : id(id_), parent(parent_), children(children_) {}

  Node() : id(nullNum), parent(nullptr), children(std::vector<int>(0)) {}

  void SetID(int id_) { id = id_; }

  void SetChildren(std::vector<int> cld) { children = cld; }

  void SetParent(Node *prt) { parent = prt; }

  int &ID() { return id; }

  int Depth() {
    int depth = 0;
    Node *ptr = Parent();
    while (ptr != nullptr) {
      ++depth;
      ptr = ptr->Parent();
    }
    return depth;
  }

  Node *Parent() { return parent; }

  std::vector<int> &ChildrenIDs() { return children; }

  std::string CategoryName() {
    if (!parent) {
      return "root";
    } else if (children.empty()) {
      return "leaf";
    }
    return "internal node";
  }

private:
  int id;
  Node *parent;
  std::vector<int> children; // owns children IDs as a vector
};

int main() {
  int n;
  std::cin.tie(0);
  std::cin >> n;

  std::vector<Node> tree(n);

  int id;
  int numOfChildren;
  std::vector<int> cld;

  for (auto &elem : tree) {
    elem = Node();
  }

  for (int i = 0; i < n; ++i) {
    std::cin >> id >> numOfChildren;
    tree[id].SetID(id);
    cld = std::vector<int>(numOfChildren);
    for (int j = 0; j < numOfChildren; ++j) {
      std::cin >> cld[j];
    }

    tree[id].SetChildren(cld);

    for (const auto &elem : cld) {
      tree[elem].SetParent(&tree[id]);
    }
  }

  for (int i = 0; i < n; ++i) {
    std::cout << "node " << i;
    std::cout << ": parent = "
              << ((!tree[i].Parent()) ? -1 : tree[i].Parent()->ID());
    std::cout << ", depth = " << tree[i].Depth();
    std::cout << ", " << tree[i].CategoryName();
    std::cout << ", [";
    for (int j = 0; j < tree[i].ChildrenIDs().size(); ++j) {
      std::cout << tree[i].ChildrenIDs()[j]
                << ((j != tree[i].ChildrenIDs().size() - 1) ? ", " : "");
    }
    std::cout << "]\\n";
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

constexpr int nullNum = -3;

class Node {
public:
  Node(int id_, Node *parent_, std::vector<int> children_)
      : id(id_), parent(parent_), children(children_) {}

  Node() : id(nullNum), parent(nullptr), children(std::vector<int>(0)) {}

  void SetID(int id_) { id = id_; }

  void SetChildren(std::vector<int> cld) { children = cld; }

  void SetParent(Node *prt) { parent = prt; }

  int &ID() { return id; }

  int Depth() {
    int depth = 0;
    Node *ptr = Parent();
    while (ptr != nullptr) {
      ++depth;
      ptr = ptr->Parent();
    }
    return depth;
  }

  Node *Parent() { return parent; }

  std::vector<int> &ChildrenIDs() { return children; }

  std::string CategoryName() {
    if (!parent) {
      return "root";
    } else if (children.empty()) {
      return "leaf";
    }
    return "internal node";
  }

private:
  int id;
  Node *parent;
  std::vector<int> children; // owns children IDs as a vector
};

int main() {
  int n;
  std::cin.tie(0);
  std::cin >> n;

  std::vector<Node> tree(n);

  int id;
  int numOfChildren;
  std::vector<int> cld;

  for (auto &elem : tree) {
    elem = Node();
  }

  for (int i = 0; i < n; ++i) {
    std::cin >> id >> numOfChildren;
    tree[id].SetID(id);
    cld = std::vector<int>(numOfChildren);
    for (int j = 0; j < numOfChildren; ++j) {
      std::cin >> cld[j];
    }

    tree[id].SetChildren(cld);

    for (const auto &elem : cld) {
      tree[elem].SetParent(&tree[i]);
    }

    // cld = std::vector<int>(0);
  }

  // std::cout << "Input\\n";

  for (int i = 0; i < n; ++i) {
    std::cout << "node " << i;
    std::cout << ": parent = "
              << ((!tree[i].Parent()) ? -1 : tree[i].Parent()->ID());
    std::cout << ", depth = " << tree[i].Depth();
    std::cout << ", " << tree[i].CategoryName();
    std::cout << ", [";
    for (int j = 0; j < tree[i].ChildrenIDs().size(); ++j) {
      std::cout << tree[i].ChildrenIDs()[j]
                << ((j != tree[i].ChildrenIDs().size() - 1) ? ", " : "");
    }
    std::cout << "]\\n";
  }
}