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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
typedef unsigned long long ll;
struct node {
  ll id;
  node *parent;
  node *marked_parent;
  ll h;
  vector<node *> child;
};
int main() {
  // freopen("/Users/shitian/Downloads/1.txt","r",stdin);
  int n, q, p;
  while (cin >> n >> q, n) {
    vector<node *> vec;
    vector<int> marked(n, 0);
    for (int i = 0; i < n; i++) {
      node *t = new node;
      vec.push_back(t);
    }
    node *no = vec[0];
    no->id = 1;
    no->parent = no;
    no->h = 1;
    no->marked_parent = vec[0];
    marked[0] = 1;
    for (int i = 2; i <= n; i++) {
      cin >> p;
      node *no = vec[i - 1];
      no->id = i;
      no->parent = vec[p - 1];
      no->h = no->parent->h + 1;
      no->marked_parent = vec[0];
      vec[p - 1]->child.push_back(no);
    }
    ll ans = 0;
    for (int i = 0; i < q; i++) {

      char que;
      int num;
      cin >> que >> num;
      if (que == 'M') {
        marked[num - 1] = 1;
        node *mp = vec[num - 1];
        queue<node *> que;
        que.push(mp);
        while (!que.empty()) {
          node *now = que.front();
          que.pop();
          // cout<<"now->id-1       "<<now->id-1<<endl;
          if (marked[now->id - 1] == 1 && now->id != num) {
            continue;
          }
          now->marked_parent = mp;
          // cout<<"now-id: "<<now->id<<"     mp->id: "<<mp->id<<endl;
          for (int i = 0; i < now->child.size(); i++) {
            que.push(now->child[i]);
          }
        }
      } else {
        ans += vec[num - 1]->marked_parent->id;
      }
    }
    cout << ans << endl;
    ;
    vec.clear();
    marked.clear();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;

struct node {
  int id;
  node *parent;
  int h;
  vector<node *> child;
};
bool com(node *n1, node *n2) { return n1->id < n2->id; }
int main() {
  // freopen("/Users/shitian/Downloads/1.txt","r",stdin);
  int n, q, p;
  while (cin >> n >> q, n) {
    vector<node *> vec;
    vector<node *> marked;
    for (int i = 0; i < n; i++) {
      node *t = new node;
      vec.push_back(t);
    }
    node *no = vec[0];
    vec[0]->id = 1;
    vec[0]->parent = no;
    vec[0]->h = 1;
    marked.push_back(no);
    for (int i = 2; i <= n; i++) {
      cin >> p;
      node *no = vec[i - 1];
      no->id = i;
      no->parent = vec[p - 1];
      no->h = no->parent->h + 1;
      vec[p - 1]->child.push_back(no);
    }
    int ans = 0;
    for (int i = 0; i < q; i++) {
      char que;
      int num;
      cin >> que >> num;
      if (que == 'M') {
        marked.push_back(vec[num - 1]);
      } else {
        node *pa = vec[num - 1]->parent;
        int found = 0;
        while (true) {
          int ma = 0;
          sort(marked.begin(), marked.end(), com);
          if (binary_search(marked.begin(), marked.end(), pa, com)) {
            ma = max(ma, pa->id);
            found = 1;
          }
          if (found) {
            ans += ma;
            goto next;
          }
          if (pa->id == 1) {
            goto next;
          }
          pa = vec[pa->id]->parent;
        }

      next:;
      }
    }
    cout << ans << endl;
    ;
    vec.clear();
    marked.clear();
  }
}