https://ac.nowcoder.com/acm/problem/13249
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> using namespace std; typedef long long ll; const int maxn = 2e5 + 777; struct Node { int to; int nxt; }G[maxn * 2]; int head[maxn]; int z; void add(int be, int en) { G[++z].to = en; G[z].nxt = head[be]; head[be] = z; } ll dp[maxn];//操作了几次 ll list[maxn]; ll ans[maxn];//向上几个祖宗 int de[maxn]; ll cns = 0; int dfs(int x, int fa) { for (int i = head[x]; i; i = G[i].nxt) { int p = G[i].to; if (p == fa) continue; dfs(p, x); ans[x] = max(ans[p] - 1, ans[x]); dp[x] += dp[p]; } if (ans[x] == 0 ) { cns++; ans[x] = list[x]; } else { list[fa] = max(list[fa], list[x] - 1); } return 0; } int main() { int n; scanf("%d", &n); int be, en; for (int i = 2; i <= n; i++) { scanf("%d", &be); de[be]++; de[i]++; add(be, i); add(i, be); } for (int i = 1; i <= n; i++) { scanf("%lld", &list[i]); } dfs(1, 0); printf("%lld ", cns); return 0; }