#include #define RAN(v) v.begin(), v.end() #define pb push_back #define lb lower_bound #define ub upper_bound using namespace std; typedef long long ll; template inline void upd1(T1& a, const T2& b) { a = a < b ? a : b; } template inline void upd2(T1& a, const T2& b) { a = b < a ? a : b; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } struct Ano { operator ll() { ll x = 0, y = 0, c = getchar(); while (c < 48) { y = c == 45; c = getchar(); } while (c > 47) { x = x*10 + c-48; c = getchar(); } return y ? -x : x; } } buf; constexpr int N = 1e5 + 5; vector t[N]; int k, c[N], f[N], w[N]; void dfs(int u) { f[u] = w[u] - 1; c[u] = 0; for (int v: t[u]) { dfs(v); c[u] += c[v]; if (f[v] == k) { ++c[u]; } else if (~f[v]) { upd2(f[u], f[v] + 1); } } } int main() { int q = buf; while (q--) { int n = buf; k = buf; for (int i = 1; i <= n; ++i) { t[i].clear(); } for (int i = 2; i <= n; ++i) { t[buf].pb(i); } for (int i = 1; i <= n; ++i) { w[i] = buf; } dfs(1); printf("%d\n", c[1] + !!~f[1]); } }