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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

//?°???\\???????????????public?????????
class Node {
public:
  int id;
  int parent;
  int depth;
  string type;
  int degree;
  vector<int> c;

public:
  Node(int id, vector<int> c) : id(id), c(c) { depth = degree = parent = -1; }
  void print();
};

void Node::print() {
  if (parent == -1)
    type = "root";
  else if (!c.size())
    type = "leaf";
  else
    type = "internal node";
  printf("node %d: parent = %d, depth = %d, %s, [", id, parent, depth,
         type.c_str());
  for (int i = 0; i < (int)c.size(); i++) {
    printf("%d", c[i]);
    if (i != (int)c.size() - 1) {
      printf(", ");
    }
  }
  printf("]\\n");
}

int search(vector<Node> &v, int id, int depth) {
  v[id].depth = depth;
  for (int i = 0; i < v[id].c.size(); i++) {
    search(v, v[id].c[i], depth + 1);
  }
}

bool asc(const Node &right, const Node &left) { return right.id < left.id; }

int main(void) {
  int n;
  cin >> n;
  vector<Node> nodes;

  //??\\?????¨
  for (int i = 0; i < n; i++) {
    int id, t;
    cin >> id >> t;
    vector<int> v(t);
    for (int j = 0; j < t; j++)
      cin >> v[j];
    Node node(id, v);
    nodes.push_back(node);
  }

  sort(nodes.begin(), nodes.end(), asc);

  //????????¨
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < nodes[i].c.size(); j++) {
      nodes[nodes[i].c[j]].parent = nodes[i].id;
    }
  }
  for (int i = 0; i < n; i++) {
    if (nodes[i].parent == -1) {
      search(nodes, i, 0);
    }
  }

  //????????¨
  for (int i = 0; i < n; i++) {
    nodes[i].print();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

//?°???\\???????????????public?????????
class Node {
public:
  int id;
  int parent;
  int depth;
  string type;
  vector<int> c;

public:
  Node(int id, vector<int> c) : id(id), c(c) {
    parent = -1;
    depth = 0;
  }
  void print();
};

void Node::print() {
  if (parent == -1)
    type = "root";
  else if (!c.size())
    type = "leaf";
  else
    type = "internal node";
  printf("node %d: parent = %d, depth = %d, %s, [", id, parent, depth,
         type.c_str());
  for (int i = 0; i < (int)c.size(); i++) {
    printf("%d", c[i]);
    if (i != (int)c.size() - 1) {
      printf(", ");
    }
  }
  printf("]\\n");
}

int search(vector<Node> v, int id, int depth) {
  for (int i = 0; i < v.size(); i++) {
    if (id == v[i].id) {
      if (v[i].parent == -1) {
        return depth;
      } else {
        return search(v, v[i].parent, depth + 1);
      }
    }
  }
}

bool asc(const Node &right, const Node &left) { return right.id < left.id; }

int main(void) {
  int n;
  cin >> n;
  vector<Node> nodes;

  //??\\?????¨
  for (int i = 0; i < n; i++) {
    int id, t;
    cin >> id >> t;
    vector<int> v(t);
    for (int j = 0; j < t; j++)
      cin >> v[j];
    Node node(id, v);
    nodes.push_back(node);
  }

  sort(nodes.begin(), nodes.end(), asc);

  //????????¨
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < nodes[i].c.size(); j++) {
      nodes[nodes[i].c[j]].parent = nodes[i].id;
    }
  }
  queue<pair<int, int>> que;
  for (int i = 0; i < n; i++) {
    if (nodes[i].parent == -1) {
      for (int j = 0; j < nodes[i].c.size(); j++) {
        que.push(make_pair(nodes[i].c[j], 1));
      }
    }
  }
  while (!que.empty()) {
    pair<int, int> q = que.front();
    que.pop();
    for (int i = 0; i < n; i++) {
      if (nodes[i].id == q.first) {
        nodes[i].depth = q.second;
        for (int j = 0; j < nodes[i].c.size(); j++) {
          que.push(make_pair(nodes[i].c[j], nodes[i].depth + 1));
        }
      }
    }
  }

  //????????¨
  for (int i = 0; i < n; i++) {
    nodes[i].print();
  }

  return 0;
}