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

#include <iostream>
#define INDEX 100000
#define NULLL -1
using namespace std;

struct Node {
  int parent;
  int left;
  int right;
};

Node node[INDEX];
int depth[INDEX];

void setDepth(int u, int p);
void printChildren(int u);

int main() {
  int i, j, n, d, id, dim, tmp, cleft;

  cin >> n;

  for (i = 0; i < n; i++) {
    node[i].parent = NULLL;
    node[i].left = NULLL;
    node[i].right = NULLL;
  }

  for (i = 0; i < n; i++) {
    cin >> id >> dim;
    for (j = 0; j < dim; j++) {
      cin >> tmp;
      if (j == 0)
        node[id].left = tmp;
      else
        node[cleft].right = tmp;
      node[tmp].parent = id;
      cleft = tmp;
    }
  }

  i = 0;
  while (1) {
    if (node[i].parent == NULLL)
      break;
    i++;
  }
  setDepth(i, 0);

  for (i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << node[i].parent
         << ", depth = " << depth[i] << ", ";
    if (node[i].parent == NULLL)
      cout << "root";
    else if (node[i].left == NULLL)
      cout << "leaf";
    else
      cout << "internal node";
    cout << ", ";
    printChildren(i);
  }

  return 0;
}

void setDepth(int u, int p) {
  depth[u] = p;
  if (node[u].right != NULLL)
    setDepth(node[u].right, p);
  if (node[u].left != NULLL)
    setDepth(node[u].left, p + 1);
}

void printChildren(int u) {
  int i = 0, cleft;
  if (node[u].left == NULLL) {
    cout << "[]\\n";
    return;
  }
  cout << "[" << node[u].left;
  cleft = node[u].left;
  while (node[cleft].right != NULLL) {
    cout << ", " << node[cleft].right;
    cleft = node[cleft].right;
  }
  cout << "]\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
struct Node {
  int id;
  int dim;
  Node *pparent;
  int *child;
  bool operator<(Node n) { return id < n.id; };
  bool operator==(Node n) { return id == n.id; };
  bool operator>(Node n) { return id > n.id; };
};

int main() {
  int i, j, k, n, depth;
  bool flag;
  Node node[100000], nullnode;
  Node *ptmpn;
  nullnode.id = -1;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d%d", &node[i].id, &node[i].dim);
    node[i].child = new int[node[i].dim];
    for (j = 0; j < node[i].dim; j++) {
      scanf("%d", &node[i].child[j]);
    }
  }
  sort(node, node + n);
  for (i = 0; i < n; i++) {
    j = 0;
    flag = false;
    while (flag == false && j < n) {
      k = 0;
      while (flag == false && k < node[j].dim) {
        if (node[j].child[k] == node[i].id)
          flag = true;
        k++;
      }
      j++;
    }
    if (flag == true)
      node[i].pparent = &node[--j];
    else
      node[i].pparent = &nullnode;
  }
  for (i = 0; i < n; i++) {
    printf("node %d: parent = %d", node[i].id, node[i].pparent->id);
    depth = 0;
    ptmpn = &node[i];
    while (ptmpn->pparent->id != -1) {
      ptmpn = ptmpn->pparent;
      depth++;
    }
    printf(", depth = %d, ", depth);
    if (depth == 0)
      printf("root, [");
    else if (node[i].dim != 0)
      printf("internal node, [");
    else
      printf("leaf, [");
    for (j = 0; j < node[i].dim; j++) {
      printf("%d", node[i].child[j]);
      if (j != node[i].dim - 1)
        printf(", ");
    }
    printf("]\\n");
  }
  return 0;
}