Language: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <set>
using namespace std;

int main() {
        int n;
        scanf("%d", &n);
        vector<vector<int>> g(n + 1);
        for (int i = 0; i < n; i ++) {
                int p;
                scanf("%d", &p);
                g[p].push_back(i + 1);
                g[i + 1].push_back(p);
        }
        n = n + 1;
        vector<int> depth(n);
        function<void (int, int, int)> dfs = [&](int u, int prev, int d) {
                depth[u] = d;
                for (auto v : g[u]) if (v != prev) {
                        dfs(v, u, d + 1);
                }
        };
        dfs(0, -1, 0);
        vector<int> cnt(n, 0);
        for (int i = 0; i < n; i ++) cnt[depth[i]] ++;
        vector<int> num(n);
        for (int i = 0; i < n; i ++) if (cnt[i] != 0) num[i] = n - cnt[i];
        vector<map<int, pair<int, int>>> dp(n); //fi -> one, se -> zero
        function<void  (int, int)> merge = [&](int u, int v) {
                if (dp[u].size() < dp[v].size()) swap(dp[u], dp[v]);
                for (auto &sub : dp[v]) {
                        int depth = sub.first;
                        int fi = dp[u][depth].first * sub.second.second + dp[u][depth].second * sub.second.first;
                        int se = dp[u][depth].first * sub.second.first + dp[u][depth].second * sub.second.second;
                        dp[u][depth].first = fi;
                        dp[u][depth].second = se;
                }
                dp[v].clear();
        };
        function<void (int, int, int)> dfs2 = [&](int u, int prev, int d) {
                dp[u][d] = make_pair(1, 1);
                for (auto v : g[u]) if (v != prev) {
                        dfs2(v, u, d + 1);
                        merge(u, v);
                }
        };
        dfs2(0, -1, 0);
        long long ans = 0;
        for (auto k : dp[0]) {
                int depth = k.first;
                ans += pow(2, num[depth]) * k.second.first;
        }
        printf("%lld\n", ans);
        return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:23:9: error: 'function' was not declared in this scope
   23 |         function<void (int, int, int)> dfs = [&](int u, int prev, int d) {
      |         ^~~~~~~~
a.cc:9:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
    8 | #include <set>
  +++ |+#include <functional>
    9 | using namespace std;
a.cc:23:37: error: expression list treated as compound expression in functional cast [-fpermissive]
   23 |         function<void (int, int, int)> dfs = [&](int u, int prev, int d) {
      |                                     ^
a.cc:23:18: error: expected primary-expression before 'void'
   23 |         function<void (int, int, int)> dfs = [&](int u, int prev, int d) {
      |                  ^~~~
a.cc:29:9: error: 'dfs' was not declared in this scope
   29 |         dfs(0, -1, 0);
      |         ^~~
a.cc:35:33: error: expression list treated as compound expression in functional cast [-fpermissive]
   35 |         function<void  (int, int)> merge = [&](int u, int v) {
      |                                 ^
a.cc:35:18: error: expected primary-expression before 'void'
   35 |         function<void  (int, int)> merge = [&](int u, int v) {
      |                  ^~~~
a.cc:46:37: error: expression list treated as compound expression in functional cast [-fpermissive]
   46 |         function<void (int, int, int)> dfs2 = [&](int u, int prev, int d) {
      |                                     ^
a.cc:46:18: error: expected primary-expression before 'void'
   46 |         function<void (int, int, int)> dfs2 = [&](int u, int prev, int d) {
      |                  ^~~~
a.cc:53:9: error: 'dfs2' was not declared in this scope
   53 |         dfs2(0, -1, 0);
      |         ^~~~