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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

int N;
int *marked_boss, *height;

struct Info {
  bool isMarked;
  vector<int> Children;
};

Info *info;

void update(int new_marked_boss_id) {

  int current_boss_id = marked_boss[new_marked_boss_id], id;
  marked_boss[new_marked_boss_id] = new_marked_boss_id;
  queue<int> Q;

  for (int i = 0; i < info[new_marked_boss_id].Children.size(); i++) {

    if (marked_boss[info[new_marked_boss_id].Children[i]] == current_boss_id) {
      marked_boss[info[new_marked_boss_id].Children[i]] = new_marked_boss_id;
      Q.push(info[new_marked_boss_id].Children[i]);
    }
  }

  while (!Q.empty()) {
    id = Q.front();
    Q.pop();

    for (int i = 0; i < info[id].Children.size(); i++) {
      if (marked_boss[info[id].Children[i]] == current_boss_id) {
        marked_boss[info[id].Children[i]] = new_marked_boss_id;
        Q.push(info[id].Children[i]);
      }
    }
  }
}

int main() {

  int Q, value;
  char buf[2];
  ll ans;

  marked_boss = new int[100001];
  height = new int[100001];
  info = (Info *)malloc(sizeof(Info) * (100001));

  while (true) {
    scanf("%d %d", &N, &Q);
    if (N == 0 && Q == 0)
      break;

    for (int i = 1; i <= N; i++) {
      marked_boss[i] = 1;
      height[i] = 0;
      info[i].isMarked = false;
      info[i].Children.clear();
    }

    info[1].isMarked = true;

    for (int i = 2; i <= N; i++) {
      scanf("%d", &value);
      info[value].Children.push_back(i);
    }

    ans = 0;

    for (int i = 0; i < Q; i++) {

      scanf("%s %d", buf, &value);

      if (buf[0] == 'Q') { //?????¨???
        ans += marked_boss[value];
      } else { //???????????????
        info[value].isMarked = true;
        update(value);
      }
    }

    printf("%lld\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

int N;
int *marked_boss, *height, *boss;

struct Info {
  vector<int> Children;
};

Info *info;

int get_boss(int id) {}

// new_boss_id??¨????????????????????????new_boss_id?????¨?§????????????????????????°???????????????new_boss?????????
void update(int new_marked_boss_id) {

  int current_boss_id = marked_boss[new_marked_boss_id], id;
  queue<int> Q;

  for (int i = 0; i < info[new_marked_boss_id].Children.size(); i++) {
    // if(marked_boss[info[new_marked_boss_id].Children[i]] == current_boss_id){
    // //????????????????????????????????¨??????????????????????????????????????????????????????????????????
    marked_boss[info[new_marked_boss_id].Children[i]] = new_marked_boss_id;
    Q.push(info[new_marked_boss_id].Children[i]);
    //}
  }

  while (!Q.empty()) {
    id = Q.front();
    Q.pop();

    for (int i = 0; i < info[id].Children.size(); i++) {
      // if(marked_boss[info[id].Children[i]] == current_boss_id){
      // //????????????????????????????????¨??????????????????????????????????????????????????????????????????
      marked_boss[info[id].Children[i]] = new_marked_boss_id;
      Q.push(info[id].Children[i]);
      //}
    }
  }
}

int main() {

  int Q, value;
  char buf[2];
  ll ans;

  marked_boss = new int[100001];
  boss = new int[100001];
  height = new int[100001];
  info = (Info *)malloc(sizeof(Info) * (100001));

  while (true) {
    scanf("%d %d", &N, &Q);
    if (N == 0 && Q == 0)
      break;

    for (int i = 1; i <= N; i++) {
      marked_boss[i] = 1;
      boss[i] = i;
      height[i] = 0;
      info[i].Children.clear();
    }

    for (int i = 2; i <= N; i++) {
      scanf("%d", &value);
      boss[i] = value;
      info[value].Children.push_back(i); // value?????????i???????????????
    }

    ans = 0;

    for (int i = 0; i < Q; i++) {

      scanf("%s %d", buf, &value);

      if (buf[0] == 'Q') { //?????¨???
        ans += marked_boss[value];
      } else { //???????????????
        update(value);
      }
    }

    printf("%lld\\n", ans);
  }

  return 0;
}