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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vp;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

vi d;
vector<vi> g;

void dfs(int now, int a, int old) {
  if (d[now] == old)
    d[now] = a;
  else
    return;
  rep(i, g[now].size()) dfs(g[now][i], a, old);
}

signed main(void) {
  int n, q;
  while (1) {
    scanf("%lld %lld", &n, &q);
    if (n == 0)
      return 0;
    g = vector<vi>(n);
    loop(i, 1, n) {
      int a;
      scanf("%lld", &a);
      a--;
      g[a].push_back(i);
    }
    d = vi(n, 1);
    int ans = 0;
    while (q--) {
      char s[5];
      scanf("%s", s);
      int v;
      scanf("%lld", &v);
      v--;
      if (s[0] == 'M') {
        dfs(v, v + 1, d[v]);
      } else {
        ans += d[v];
      }
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vp;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

vi d;
vector<vi> g;

void dfs(int now, int a) {
  d[now] = a;
  rep(i, g[now].size()) dfs(g[now][i], a);
}

signed main(void) {
  int n, q;
  while (1) {
    scanf("%lld %lld", &n, &q);
    if (n == 0)
      return 0;
    g = vector<vi>(n);
    loop(i, 1, n) {
      int a;
      scanf("%lld", &a);
      a--;
      g[a].push_back(i);
    }
    d = vi(n, 1);
    int ans = 0;
    while (q--) {
      char s[5];
      scanf("%s", s);
      int v;
      scanf("%lld", &v);
      v--;
      if (s[0] == 'M') {
        dfs(v, v + 1);
      } else {
        ans += d[v];
      }
    }
    cout << ans << endl;
  }
}