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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

struct Node {
  int prnt;
  int k;
  int *c;
};

class RootedTree {
public:
  int n;
  Node *nd;
  int solve();
  int depth(int id);
  string nodetype(int id);
  int showChildren(int id);
};

int RootedTree::solve() {
  int id;
  cin >> n;

  nd = new Node[n];

  for (int i = 0; i < n; ++i) {
    nd[i].prnt = -1;
  }

  for (int i = 0; i < n; ++i) {
    cin >> id;
    cin >> nd[id].k;

    nd[id].c = new int[nd[id].k];

    for (int j = 0; j < nd[id].k; ++j) {
      cin >> nd[id].c[j];
      nd[nd[id].c[j]].prnt = id;
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << "node " << i << ": parent = ";
    cout << nd[i].prnt << ", depth = ";
    cout << depth(i) << ", ";
    cout << nodetype(i) << ", ";
    showChildren(i);
    cout << endl;
  }

  return 0;
}

int RootedTree::depth(int id) {
  int ret = 0;

  while ((id = nd[id].prnt) != -1)
    ++ret;

  return ret;
}

string RootedTree::nodetype(int id) {
  if (nd[id].prnt == -1)
    return "root";

  if (nd[id].k == 0)
    return "leaf";
  else
    return "internal node";
}

int RootedTree::showChildren(int id) {
  if (nd[id].k == 0)
    cout << "[]";
  else {
    cout << "[";
    for (int i = 0; i < nd[id].k; ++i) {
      if (i)
        cout << ", ";
      cout << nd[id].c[i];
    }
    cout << "]";
  }

  return 0;
}

int main() {

  RootedTree rt;

  rt.solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

struct Node {
  int id;
  int k;
  int *c;
};

class RootedTree {
public:
  int n;
  Node *nd;
  int solve();
  int parent(int id);
  int depth(int id);
  string nodetype(int id);
  int showChildren(int id);
};

int RootedTree::solve() {
  cin >> n;

  nd = new Node[n];

  for (int i = 0; i < n; ++i) {
    cin >> nd[i].id;
    cin >> nd[i].k;

    nd[i].c = new int[nd[i].k];

    for (int j = 0; j < nd[i].k; ++j)
      cin >> nd[i].c[j];
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (nd[j].id == i) {
        cout << "node " << nd[j].id << ": parent = ";
        cout << parent(nd[j].id) << ", depth = ";
        cout << depth(nd[j].id) << ", ";
        cout << nodetype(nd[j].id) << ", ";
        showChildren(nd[j].id);
        cout << endl;
      }
    }
  }

  return 0;
}

int RootedTree::parent(int id) {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < nd[i].k; ++j) {
      if (nd[i].c[j] == id)
        return nd[i].id;
    }
  }

  return -1;
}

int RootedTree::depth(int id) {
  int ret = 0;
  int d = id;

  while ((d = parent(d)) != -1)
    ++ret;

  return ret;
}

string RootedTree::nodetype(int id) {
  if (parent(id) == -1)
    return "root";

  for (int i = 0; i < n; ++i) {
    if (nd[i].id == id) {
      if (nd[i].k == 0)
        return "leaf";
      else
        return "internal node";
    }
  }
}

int RootedTree::showChildren(int id) {
  for (int i = 0; i < n; ++i) {
    if (nd[i].id == id) {
      if (nd[i].k == 0)
        cout << "[]";
      else {
        cout << "[";
        for (int j = 0; j < nd[i].k; ++j) {
          if (j)
            cout << ", ";
          cout << nd[i].c[j];
        }
        cout << "]";
      }
    }
  }

  return 0;
}

int main() {

  RootedTree rt;

  rt.solve();

  return 0;
}