#include #include #include #include #include #include #include const int MAX_N = 100010; int T, n, k, m, d, p[MAX_N], a[MAX_N], ban[MAX_N]; std::vector e[MAX_N]; std::pair c[MAX_N]; void dfs1(int x, int depth) { if (a[x]) { c[++m] = std::make_pair(depth, x); d = std::max(d, depth); } for (int y : e[x]) dfs1(y, depth + 1); } void dfs2(int x) { if (ban[x]) return; ban[x] = 1; for (int y : e[x]) dfs2(y); } int main() { std::ios::sync_with_stdio(false); std::cin >> T; while (T--) { std::cin >> n >> k; for (int i = 2; i <= n; i++) { std::cin >> p[i]; e[p[i]].push_back(i); } for (int i = 1; i <= n; i++) std::cin >> a[i]; m = d = 0; dfs1(1, 1); if (k >= d) { std::cout << 1 << std::endl; } else { std::sort(c + 1, c + m + 1); for (int i = 1; i <= n; i++) ban[i] = 0; int ans = 0; for (int i = m; i >= 1; i--) { int x = c[i].second; if (ban[x]) continue; ans++; for (int j = 1; j <= k && x != 1; j++) x = p[x]; dfs2(x); } std::cout << ans << std::endl; } for (int i = 1; i <= n; i++) e[i].clear(); } return 0; }