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

#include <stdio.h>
int n;
int id;
int t[100000];
int u[100000];
int v[100000];
int w[100000];
int tmp[100000];

void setdepth(int x) {
  if (t[x] == -1) {
    w[x] = 0;
    return;
  }
  if (w[x] != -1)
    return;
  setdepth(t[x]);
  w[x] = w[t[x]] + 1;
}

void init() {
  int i;
  for (i = 0; i < 100000; i++) {
    t[i] = -1;
    u[i] = -1;
    v[i] = -1;
    w[i] = -1;
  }
}

int main() {
  int i, j, k, pos;

  init();
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &id);
    scanf("%d", &k);
    for (j = 0; j < k; j++) {
      scanf("%d", &tmp[j]);
      t[tmp[j]] = id;
      if (j > 0)
        v[tmp[j - 1]] = tmp[j];
    }
    if (k)
      u[id] = tmp[0];
  }

  for (i = 0; i < n; i++) {
    setdepth(i);
    printf("node %d: parent = %d, depth = %d, ", i, t[i], w[i]);
    if (w[i] == 0)
      printf("root, ");
    else if (u[i] == -1)
      printf("leaf, ");
    else
      printf("internal node, ");
    pos = u[i];
    printf("[");
    if (pos != -1) {
      while (1) {
        printf("%d", pos);
        pos = v[pos];
        if (pos == -1)
          break;
        printf(", ");
      }
    }
    printf("]\\n");
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MAX 100005

using namespace std;

typedef struct {
  int idx;
  int v;
  int parent;
  int dep;
  vector<int> children;
} node;

// O(n)...線形探索...しかし今回のデータだとO(1)?
int findParent(vector<node *> tree) {
  int n = tree.size();
  for (int i = 0; i < n; i++) {
    if (tree[i]->parent == -1) {
      return i;
    }
  }
  return -1;
}

void culcurateDepth(vector<node *> tree) {
  int pid = findParent(tree);
  node *p = tree[pid];
  p->dep = 0;
  queue<int> Q;
  for (int i = 0; i < p->v; i++) {
    Q.push(p->children[i]);
  }
  while (!Q.empty()) {
    p = tree[Q.front()];
    p->dep = tree[p->parent]->dep + 1;
    for (int i = 0; i < p->v; i++) {
      Q.push(p->children[i]);
    }
    Q.pop();
  }
  /*
  while(tree[idx] -> parent != -1){
      idx = tree[idx] -> parent;
      dep++;
  }
  */
}

char *judgeKind(vector<node *> tree, int idx) {
  if (tree[idx]->parent == -1) {
    char *kind = new char[5];
    kind = "root";
    return kind;
  } else if (tree[idx]->v == 0) {
    char *kind = new char[5];
    kind = "leaf";
    return kind;
  } else {
    char *kind = new char[14];
    kind = "internal node";
    return kind;
  }
}

int main() {
  vector<node *> tree;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    node *x = new node;
    x->parent = -1;
    tree.push_back(x);
  }
  tree[0]->children.reserve(MAX);
  for (int i = 0; i < n; i++) {
    int idx, v;
    scanf("%d %d", &idx, &v);
    tree[idx]->idx = idx;
    tree[idx]->v = v;
    for (int j = 0; j < v; j++) {
      int cid;
      scanf("%d", &cid);
      tree[idx]->children.push_back(cid);
      tree[cid]->parent = idx;
    }
  }
  // ここまでで木の生成はできた

  culcurateDepth(tree);
  for (int i = 0; i < n; i++) {
    char *kind = judgeKind(tree, i);
    printf("node %d: parent = %d, depth = %d, %s, [", tree[i]->idx,
           tree[i]->parent, tree[i]->dep, kind);
    if (tree[i]->v > 0) {
      for (int j = 0; j < tree[i]->v - 1; j++) {
        printf("%d, ", tree[i]->children[j]);
      }
      printf("%d", tree[i]->children[tree[i]->v - 1]);
    }
    printf("]\\n");
  }
  return 0;
}