Problem p03499 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;

inline bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
inline bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * fh;
}

void File() {
#ifdef zjp_shadow
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
#endif
}

typedef long long ll;
typedef pair<ll, ll> pll;
#define fir first
#define sec second
#define mp make_pair
const ll Mod = 1e9 + 7;
const int N = 201000;

ll ans = 0;
ll fpm(ll x, ll power) {
  ll res = 1;
  for (; power; power >>= 1, (x *= x) %= Mod)
    if (power & 1)
      (res *= x) %= Mod;
  return res;
}

int fa[N], n, tot[N];
vector<int> G[N];

int id[N], num = 0, d[N];
vector<pll> dp[N];

ll All[N], Zero[N], One[N];

void Dfs(int u) {
  int son = n + 1;
  for (int v : G[u]) {
    Dfs(v);
    if (d[v] > d[son])
      son = v;
  }
  if (son != n + 1)
    id[u] = id[son], d[u] = d[son] + 1;
  else
    id[u] = ++num;
  dp[id[u]].push_back(mp(1, 1));
  if (son == n + 1)
    return;

  For(i, 0, d[u] - 1) All[i] = 1, Zero[i] = 1, One[i] = 0;

  int nowdep;
  for (int v : G[u])
    For(i, 0, d[v]) {
      nowdep = (d[son] - d[v]) + i;
      pll sta = dp[id[v]][i];
      (All[nowdep] *= (sta.fir + sta.sec)) %= Mod;
      (Zero[nowdep] *= sta.fir) %= Mod;
    }

  for (int v : G[u])
    For(i, 0, d[v]) {
      nowdep = (d[son] - d[v]) + i;
      pll sta = dp[id[v]][i];
      (One[nowdep] +=
       Zero[nowdep] * fpm(sta.fir, Mod - 2) % Mod * sta.sec % Mod) %= Mod;
    }

  For(i, 0, d[u] - 1) {
    dp[id[u]][i].fir = (All[i] - One[i] + Mod) % Mod;
    dp[id[u]][i].sec = One[i];
  }
}

int dep[N];

int main() {
  File();
  n = read();
  For(i, 1, n) {
    fa[i] = read();
    G[fa[i]].push_back(i);
    dep[i] = dep[fa[i]] + 1;
    ++tot[dep[i]];
  }
  ++tot[0];
  d[n + 1] = -1;

  Dfs(0);

  For(i, 0, d[0]) {
    pll sta = dp[id[0]][i];
    (ans += sta.sec * fpm(2, n + 1 - tot[d[0] - i]) % Mod) %= Mod;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;

inline bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
inline bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * fh;
}

void File() {
#ifdef zjp_shadow
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
#endif
}

typedef long long ll;
typedef pair<ll, ll> pll;
#define fir first
#define sec second
#define mp make_pair
const ll Mod = 1e9 + 7;
const int N = 201000;

ll ans = 0;
ll fpm(ll x, ll power) {
  ll res = 1;
  for (; power; power >>= 1, (x *= x) %= Mod)
    if (power & 1)
      (res *= x) %= Mod;
  return res;
}

int fa[N], n, tot[N];
vector<int> G[N];

int id[N], num = 0, d[N];
vector<pll> dp[N];

ll All[N], Zero[N], One[N];

void Dfs(int u) {
  int son = n + 1;
  for (int v : G[u]) {
    Dfs(v);
    if (d[v] > d[son])
      son = v;
  }
  if (son != n + 1)
    id[u] = id[son], d[u] = d[son] + 1;
  else
    id[u] = ++num;
  dp[id[u]].push_back(mp(1, 1));

  if ((int)G[u].size() == 1)
    return;

  For(i, 0, d[u] - 1) All[i] = 1, Zero[i] = 1, One[i] = 0;

  int nowdep;
  for (int v : G[u])
    For(i, 0, d[v]) {
      nowdep = (d[son] - d[v]) + i;
      pll sta = dp[id[v]][i];
      (All[nowdep] *= (sta.fir + sta.sec)) %= Mod;
      (Zero[nowdep] *= sta.fir) %= Mod;
    }

  for (int v : G[u])
    For(i, 0, d[v]) {
      nowdep = (d[son] - d[v]) + i;
      pll sta = dp[id[v]][i];
      (One[nowdep] +=
       Zero[nowdep] * fpm(sta.fir, Mod - 2) % Mod * sta.sec % Mod) %= Mod;
    }

  For(i, 0, d[u] - 1) {
    dp[id[u]][i].fir = (All[i] - One[i] + Mod) % Mod;
    dp[id[u]][i].sec = One[i];
  }
}

int dep[N];

int main() {
  File();
  n = read();
  For(i, 1, n) {
    fa[i] = read();
    G[fa[i]].push_back(i);
    dep[i] = dep[fa[i]] + 1;
    ++tot[dep[i]];
  }
  ++tot[0];
  d[n + 1] = -1;

  Dfs(0);

  For(i, 0, d[0]) {
    pll sta = dp[id[0]][i];
    (ans += sta.sec * fpm(2, n + 1 - tot[d[0] - i]) % Mod) %= Mod;
  }
  printf("%lld\n", ans);
  return 0;
}