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

////////////////////////////////////////
// ALDS1_7_A:   Rooted Trees
//
// left-child, right-sibling representation
//??§????????°???Node???????????????????????§?°?????????§??????
////////////////////////////////////////

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map> //???????????\\??¢??°
#include <fstream> //ifstream, ofstream

// #define NDEBUG //NDEBUG???#include
// <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???
//????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUT2TEXTFILE????????????????????¬???????????????????????????????????¢????????????
// #define OUTPUT2TEXTFILE
// //*******************************************************************************************************************************************
#ifdef OUTPUT2TEXTFILE
#define dout                                                                   \\
  outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
// TEST??????????????????????????¨???outputfile??????????????????????????????
#define OUTPUTFILENAME "output.txt"
ofstream outputfile(OUTPUTFILENAME);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A)                                                                \\
  dout << setw(3) << (A) << " " // << setw(3) ??????????????\\????????????
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)

#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;

const int INF = 1e9 - 1;

class Node;

vector<Node> node;

class Node {
public:
  Node() {
    parent = -1;
    leftChild = -1;
    rightSibling = -1;
  }

  int parent;
  int leftChild;
  int rightSibling;

  int depth() {
    int p = this->parent;
    int d = 0;

    while (p != -1) {
      d++;
      p = node[p].parent;
    }

    return d;
  }

  void printChild() {
    int c = this->leftChild;
    if (c == -1)
      return;
    else {
      cout << c;
    }

    c = node[c].rightSibling;

    while (c != -1) {
      cout << ", " << c;
      c = node[c].rightSibling;
    }
  }
};

int main() {
  int N;
  cin >> N;
  rep(i, 0, N) node.push_back(*new Node());

  // read input data
  int p, numOfChild, c, c_prev = -1;
  rep(i, 0, N) {
    //        dout<<"------";
    //        disp(i);
    cin >> p >> numOfChild;

    rep(j, 0, numOfChild) {
      //            disp(j);
      cin >> c;
      //            disp(c);

      if (j == 0)
        node[p].leftChild = c;
      else {
        assert(c_prev != -1);
        node[c_prev].rightSibling = c;
      }

      node[c].parent = p;
      c_prev = c;
    }
  }

  //    //test display
  //    rep(i,0,N) {
  //        disP(i);
  //        disP(node[i].leftChild); disP(node[i].rightSibling);
  //        disP(node[i].parent); dout << endl;
  //    }

  // output
  rep(i, 0, N) {
    cout << "node " << i << ": parent = " << node[i].parent
         << ", depth = " << node[i].depth() << ", "
         << (node[i].parent == -1
                 ? "root"
                 : (node[i].leftChild == -1 ? "leaf" : "internal node"))
         << ", [";

    node[i].printChild();

    cout << "]" << endl;
  }

#ifdef OUTPUT2TEXTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}

Error: TLE

Fixed submission:

////////////////////////////////////////
// ALDS1_7_A:   Rooted Trees
//
//
////////////////////////////////////////

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map> //???????????\\??¢??°
#include <fstream> //ifstream, ofstream

// #define NDEBUG //NDEBUG???#include
// <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???
//????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUT2TEXTFILE????????????????????¬???????????????????????????????????¢????????????
// #define OUTPUT2TEXTFILE
// //*******************************************************************************************************************************************
#ifdef OUTPUT2TEXTFILE
#define dout                                                                   \\
  outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
// TEST??????????????????????????¨???outputfile??????????????????????????????
#define OUTPUTFILENAME "output.txt"
ofstream outputfile(OUTPUTFILENAME);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A)                                                                \\
  dout << setw(3) << (A) << " " // << setw(3) ??????????????\\????????????
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)

#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;

const int INF = 1e9 - 1;

class DoublyChainedTreeNode {
public:
  DoublyChainedTreeNode(int i) {
    id = i;
    parent = NULL;
    leftChild = NULL;
    rightSibling = NULL;
  }

  int id;
  DoublyChainedTreeNode *parent;
  DoublyChainedTreeNode *leftChild;
  DoublyChainedTreeNode *rightSibling;
};

class DoublyChainedTree {
public:
  DoublyChainedTree(DoublyChainedTreeNode *r) { root = r; }

  DoublyChainedTreeNode *root;
};

class Node {
public:
  Node(int i, Node *p) {
    id = i;
    parent = p;
  }

  //    ~Node() {
  //        delete parent;
  //    }

  int id;
  Node *parent;
  vector<Node *> child;

  static Node *placeXunderP(
      Node *x,
      Node *
          p) { // p??????????????°??????????????????id???x??¨?????´????????????????????£?????´??????????????¨x????????????????????????????????°NULL?????????
    Node *ret = NULL;

    // id??¨?????´???????????????????????°???????????????????????§???????????????????????????????????£???????????±?????????????????????????????????
    //        dout << "---placeXunderP\\n";
    //        disp(x->id);
    //        disp(p->id);

    if (p->id ==
        x->id) { // x????????§???p??¨????????????????????????????????????????????£?????´??????
      //            dout << "child.erase\\n";
      //            disp(p->parent->id);
      //            int findchild = findChild(x->id, p->parent);
      //            disp(findchild);

      int i = findChild(x->id, p->parent);
      assert(i != -1);

      p->parent->child.erase(p->parent->child.begin() +
                             i); // p???????????????????????????p???????????????

      //            dout << "pushback\\n";

      p->parent->child.insert(
          p->parent->child.begin() + i,
          x); //?????????x???p?????????????????????????????????????????????
      x->parent =
          p->parent; //????????¨???x???????????±???p????????§??´??°???????????¨???????????????

      delete p; //??????x???????????§??????p???????????????

      return x;
    }

    if (p->child.empty())
      return NULL;

    // p?????????????????????????????°????????????
    for (vector<Node *>::iterator itr = p->child.begin(); itr != p->child.end();
         itr++) {
      ret = placeXunderP(x, *itr);
      if (ret != NULL)
        break; // replace????????????????????????NULL??\\???????????£??????????????§break??????????????????????????????????????????????????????
    }

    return ret;
  }

  static int findChild(
      int idKey,
      Node *
          p) { // p??????????????????id=idKey????????????????????????????????????????????°???1?????????
    //        dout << "---findChild\\n";
    //        disp(idKey);
    //        disp(p->id);

    for (vector<Node *>::iterator itr = p->child.begin(); itr != p->child.end();
         itr++) {
      if ((*itr)->id == idKey) {
        return itr - p->child.begin();
      }
    }

    return -1;
  }

  static void printChild(
      Node *
          x) { // x???????????¨????????????????????????????????????????????°????????????
    assert(x != NULL);

    if (x->child.empty()) {
      dout << "[" << x->id << "]";
      dout << endl;
      return; //?????????????????´??????return
    } else {
      dout << "[" << x->id << "]";

      for (vector<Node *>::iterator itr = x->child.begin();
           itr != x->child.end(); itr++) {
        dout << " " << (*itr)->id;
      }
      dout << endl;

      //??????????????´??????????????????????????°????????????
      for (vector<Node *>::iterator itr = x->child.begin();
           itr != x->child.end(); itr++) {
        printChild(*itr);
      }
    }
  }

  static Node *findNodeByID(
      int idKey,
      Node *
          p) { // p??\\?????????idKey??????????????¢????????????????????????????????????????????????

    //        dout <<"--------findNodeByID\\n";
    //        disp(idKey);
    //        disp(p->id);
    //        disp(p->child.empty());

    Node *ret = NULL;

    if (p->id == idKey)
      return p;

    if (p->child.empty())
      return NULL;

    // p?????????????????????????????°????????????
    for (vector<Node *>::iterator itr = p->child.begin(); itr != p->child.end();
         itr++) {
      ret = findNodeByID(idKey, *itr);
      if (ret != NULL)
        break; //???????????£??????NULL??\\???????????£??????????????§break??????????????????????????????????????????????????????
    }

    return ret;
  }

  int depth() {
    Node *p = this;
    int d = 0;

    while (p->parent != NULL) { //???????????????????????§????????????
      d++;
      p = p->parent;
    }

    return d;
  }
};

class Tree {
public:
  Tree(Node *r) { root = r; }

  Node *root;
};

int main() {
  int NumOfNode;
  cin >> NumOfNode;

  int id, numOfChild, c;
  Node *ptr, *res;

  queue<Node *> waitingToBePlaced;

  // 1???????????£??¨??¨????????????????????§tree??¨??????
  cin >> id >> numOfChild;
  Tree tree(new Node(id, NULL)); //????????¨????§????????????????

  rep(j, 0, numOfChild) {
    cin >> c;
    tree.root->child.push_back(new Node(c, tree.root));
  }

  //    //test display
  //    dout << "------------------\\n";
  //    Node::printChild(tree.root);

  rep(i, 1, NumOfNode) {

    //        dout << "------------------\\n";
    //        disp(i);

    // 1???????????????
    cin >> id >> numOfChild;
    ptr = new Node(
        id, NULL); //???????????????ptr???tree??????????????????????????\\??????
    rep(j, 0, numOfChild) { // ptr???????????±???????????????
      cin >> c;
      ptr->child.push_back(new Node(c, ptr));
    }

    //?????????tree????????????????????§????????????
    res = Node::placeXunderP(ptr, tree.root);

    if (res ==
        NULL) { //??§???????????£?????´???????????????tree????????????????????§????????????
      res = Node::placeXunderP(tree.root, ptr);
      if (res !=
          NULL) { //?????????????????§????????´???????????????????????´???
        tree.root = ptr;
      } else { //????????§???????????§???????????£?????´?????????????????\\????????\\????????????
        waitingToBePlaced.push(ptr);
        //                dout << ptr->id << " is queued\\n";
      }
    }

    //        Node::printChild(tree.root);
    //        dout << "------------------\\n";
  }

  //?????\\???waitingToBePlaced????£??????????????????????????????????????????????????????§????????????????????????
  while (not waitingToBePlaced.empty()) {
    ptr = waitingToBePlaced.front();
    waitingToBePlaced.pop();

    //?????????tree????????????????????§????????????
    res = Node::placeXunderP(ptr, tree.root);

    if (res ==
        NULL) { //??§???????????£?????´???????????????tree????????????????????§????????????
      res = Node::placeXunderP(tree.root, ptr);
      if (res !=
          NULL) { //?????????????????§????????´???????????????????????´???
        tree.root = ptr;
      } else { //????????§???????????§???????????£?????´?????????????????\\????????\\????????????
        waitingToBePlaced.push(ptr);
        //                dout << ptr->id << " is queued again\\n";
      }
    }

    //        Node::printChild(tree.root);
    //        dout << "------------------\\n";
  }

  // tree????§????????????????????????§??????????????±?????????????????????
  rep(i, 0, NumOfNode) {
    ptr = Node::findNodeByID(i, tree.root);
    assert(ptr != NULL);

    cout << "node " << ptr->id
         << ": parent = " << (ptr->parent == NULL ? -1 : ptr->parent->id)
         << ", depth = " << ptr->depth() << ", "
         << (ptr->parent == NULL
                 ? "root"
                 : (ptr->child.empty() ? "leaf" : "internal node"))
         << ", [";

    for (vector<Node *>::iterator itr = ptr->child.begin();
         itr != ptr->child.end(); itr++) {
      if (itr == ptr->child.begin()) {
        cout << (*itr)->id;
      } else {
        cout << ", " << (*itr)->id;
      }
    }

    cout << "]\\n";
  }

#ifdef OUTPUT2TEXTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}