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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct TNode {
  int Id;
  TNode *Parent;
  TNode *NextSibling;
  TNode *FirstChild;
};

enum TNodeType { Root, InternalNode, Leaf };

void Create(int Id, vector<TNode *> &Nodes) {
  TNode *Node = new TNode;
  *Node = {Id, NULL, NULL, NULL};
  Nodes[Id] = Node;
}

void CreateRelationParent(TNode *&ParentNode, TNode *&ChildNode) {
  ParentNode->FirstChild = ChildNode;
  ChildNode->Parent = ParentNode;
}

void CreateRelationLastChild(TNode *&CurrentLastChild, TNode *&ChildNode) {
  CurrentLastChild->NextSibling = ChildNode;
  ChildNode->Parent = CurrentLastChild->Parent;
}

vector<TNode *> Input() {
  int n;
  cin >> n;
  vector<TNode *> Nodes(n, NULL);
  for (int i = 0; i < n; i++) {
    int Id;
    cin >> Id;
    if (Nodes[Id] == NULL) {
      Create(Id, Nodes);
    }
    int ChildCount;
    cin >> ChildCount;
    TNode *CurrentLastChild;
    for (int j = 0; j < ChildCount; j++) {
      int ChildId;
      cin >> ChildId;
      if (Nodes[ChildId] == NULL) {
        Create(ChildId, Nodes);
      }
      if (Nodes[Id]->FirstChild == NULL) {
        CreateRelationParent(Nodes[Id], Nodes[ChildId]);
      } else {
        CreateRelationLastChild(CurrentLastChild, Nodes[ChildId]);
      }
      CurrentLastChild = Nodes[ChildId];
    }
  }
  return Nodes;
}

int GetDepth(TNode *Node) {
  int depth = 0;
  while (Node->Parent) {
    depth++;
    Node = Node->Parent;
  }
  return depth;
}

TNodeType GetType(TNode *Node) {
  if (Node->Parent == NULL) {
    return Root;
  } else if (Node->FirstChild == NULL) {
    return Leaf;
  } else {
    return InternalNode;
  }
}

void OutputChildren(TNode *Node) {
  if (Node->FirstChild == NULL) {
    return;
  }
  Node = Node->FirstChild;
  cout << Node->Id;
  while (Node->NextSibling) {
    Node = Node->NextSibling;
    cout << ", " << Node->Id;
  }
}

void Output(const vector<TNode *> &Nodes) {
  for (unsigned int i = 0; i < Nodes.size(); i++) {
    cout << "node " << Nodes[i]->Id << ": ";
    if (Nodes[i]->Parent) {
      cout << "parent = " << Nodes[i]->Parent->Id << ", ";
    } else {
      cout << "parent = -1, ";
    }
    cout << "depth = " << GetDepth(Nodes[i]) << ", ";
    if (GetType(Nodes[i]) == Root) {
      cout << "root, ";
    } else if (GetType(Nodes[i]) == InternalNode) {
      cout << "internal node, ";
    } else {
      cout << "leaf, ";
    }
    cout << "[";
    OutputChildren(Nodes[i]);
    cout << "]" << endl;
  }
}

int main() {
  vector<TNode *> Nodes = Input();
  Output(Nodes);
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct TNode {
  int Id;
  TNode *Parent;
  TNode *NextSibling;
  TNode *FirstChild;
};

enum TNodeType { Root, InternalNode, Leaf };

void Create(int Id, vector<TNode *> &Nodes) {
  TNode *Node = new TNode;
  *Node = {Id, NULL, NULL, NULL};
  Nodes[Id] = Node;
}

void CreateRelation(TNode *&ParentNode, TNode *&ChildNode) {
  if (ParentNode->FirstChild == NULL) {
    ParentNode->FirstChild = ChildNode;
  } else {
    TNode *Child = ParentNode->FirstChild;
    while (Child->NextSibling != NULL) {
      Child = Child->NextSibling;
    }
    Child->NextSibling = ChildNode;
  }
  ChildNode->Parent = ParentNode;
}

vector<TNode *> Input() {
  int n;
  cin >> n;
  vector<TNode *> Nodes(n, NULL);
  for (int i = 0; i < n; i++) {
    int Id;
    cin >> Id;
    if (Nodes[Id] == NULL) {
      Create(Id, Nodes);
    }
    int ChildCount;
    cin >> ChildCount;
    for (int j = 0; j < ChildCount; j++) {
      int ChildId;
      cin >> ChildId;
      if (Nodes[ChildId] == NULL) {
        Create(ChildId, Nodes);
      }
      CreateRelation(Nodes[Id], Nodes[ChildId]);
    }
  }
  return Nodes;
}

int GetDepth(TNode *Node) {
  int depth = 0;
  while (Node->Parent) {
    depth++;
    Node = Node->Parent;
  }
  return depth;
}

TNodeType GetType(TNode *Node) {
  if (Node->Parent == NULL) {
    return Root;
  } else if (Node->FirstChild == NULL) {
    return Leaf;
  } else {
    return InternalNode;
  }
}

void OutputChildren(TNode *Node) {
  if (Node->FirstChild == NULL) {
    return;
  }
  Node = Node->FirstChild;
  cout << Node->Id;
  while (Node->NextSibling) {
    Node = Node->NextSibling;
    cout << ", " << Node->Id;
  }
}

void Output(const vector<TNode *> &Nodes) {
  for (unsigned int i = 0; i < Nodes.size(); i++) {
    cout << "node " << Nodes[i]->Id << ": ";
    if (Nodes[i]->Parent) {
      cout << "parent = " << Nodes[i]->Parent->Id << ", ";
    } else {
      cout << "parent = -1, ";
    }
    cout << "depth = " << GetDepth(Nodes[i]) << ", ";
    if (GetType(Nodes[i]) == Root) {
      cout << "root, ";
    } else if (GetType(Nodes[i]) == InternalNode) {
      cout << "internal node, ";
    } else {
      cout << "leaf, ";
    }
    cout << "[";
    OutputChildren(Nodes[i]);
    cout << "]" << endl;
  }
}

int main() {
  vector<TNode *> Nodes = Input();
  Output(Nodes);
  return 0;
}